Skip to content

Commit

Permalink
Refactored SailGraph and LinkedDataSailGraph for compatibility with m…
Browse files Browse the repository at this point in the history
…anageable thread-local connections. This should be a definitive fix to the 'danging connections' issue.
  • Loading branch information
joshsh committed Jul 26, 2011
1 parent 5b347d4 commit 9cef69b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 48 deletions.
Expand Up @@ -30,6 +30,7 @@
* A Blueprints implementation of the RDF-based Sail interfaces by Aduna (http://openrdf.org).
*
* @author Marko A. Rodriguez (http://markorodriguez.com)
* @author Joshua Shinavier (http://fortytwo.net)
*/
public class SailGraph implements TransactionalGraph {

Expand All @@ -53,10 +54,21 @@ public static RDFFormat getFormat(final String format) {
return ret;
}

protected final List<ThreadLocal<SailConnection>> connections = new LinkedList<ThreadLocal<SailConnection>>();
private final List<SailConnection> connections = new LinkedList<SailConnection>();

protected Sail rawGraph;
protected final ThreadLocal<SailConnection> sailConnection = createConnection();
protected final Sail rawGraph;

protected final ThreadLocal<SailConnection> sailConnection = new ThreadLocal<SailConnection>() {
protected SailConnection initialValue() {
SailConnection sc = null;
try {
sc = createConnection();
} catch (SailException e) {
e.printStackTrace(System.err);
}
return sc;
}
};

private final ThreadLocal<Boolean> inTransaction = new ThreadLocal<Boolean>() {
protected Boolean initialValue() {
Expand All @@ -76,16 +88,11 @@ protected Mode initialValue() {
* @param sail a not-yet-initialized Sail instance
*/
public SailGraph(final Sail sail) {
this.startSail(sail);
}

public SailGraph() {
}

protected void startSail(final Sail sail) {
try {
PropertyConfigurator.configure(SailGraph.class.getResource(LOG4J_PROPERTIES));
} catch (Exception e) {
// TODO: uncomment this?
//e.printStackTrace(System.err);
}
try {
this.rawGraph = sail;
Expand Down Expand Up @@ -274,66 +281,44 @@ public void clear() {
}
}

private synchronized ThreadLocal<SailConnection> createConnection() {
try {
cleanupConnections();

ThreadLocal<SailConnection> tl = new ThreadLocal<SailConnection>() {
protected SailConnection initialValue() {
try {
return rawGraph.getConnection();
} catch (SailException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
};
private synchronized SailConnection createConnection() throws SailException {
cleanupConnections();

connections.add(tl);
SailConnection sc = rawGraph.getConnection();

// Force creation of the connection (otherwise, it may be created at shutdown time, leading to weird behavior).
//tl.get();
connections.add(sc);

return tl;
} catch (SailException e) {
throw new RuntimeException(e.getMessage(), e);
}
return sc;
}

private void cleanupConnections() throws SailException {
Collection<ThreadLocal<SailConnection>> toRemove = new LinkedList<ThreadLocal<SailConnection>>();
Collection<SailConnection> toRemove = new LinkedList<SailConnection>();

for (ThreadLocal<SailConnection> tl : connections) {
SailConnection sc = tl.get();
if (null == sc || !sc.isOpen()) {
toRemove.add(tl);
for (SailConnection sc : connections) {
if (!sc.isOpen()) {
toRemove.add(sc);
}
}

for (ThreadLocal<SailConnection> tl : toRemove) {
connections.remove(tl);
for (SailConnection sc : toRemove) {
connections.remove(sc);
}
}

private void closeAllConnections() throws SailException {
for (ThreadLocal<SailConnection> tl : connections) {
SailConnection sc = tl.get();
for (SailConnection sc : connections) {
if (null != sc) {
if (sc.isOpen()) {
sc.rollback();
sc.close();
}

tl.remove();
}
}
}

public synchronized void shutdown() {
try {
closeAllConnections();

//this.sailConnection.get().close();
//this.sailConnection.remove();
this.rawGraph.shutDown();
} catch (Throwable e) {
throw new RuntimeException(e.getMessage(), e);
Expand Down
Expand Up @@ -11,11 +11,13 @@
public class LinkedDataSailGraph extends SailGraph {

public LinkedDataSailGraph(final SailGraph storageGraph) {
try {
super(createSail(storageGraph));
}

private static Sail createSail(final SailGraph storageGraph) {
try {
Ripple.initialize();
final Sail sail = new LinkedDataSail(storageGraph.getRawGraph());
sail.initialize();
this.startSail(sail);
return new LinkedDataSail(storageGraph.getRawGraph());
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
Expand Down

0 comments on commit 9cef69b

Please sign in to comment.