diff --git a/README.markdown b/README.markdown
index 36b9c62..9abb91a 100644
--- a/README.markdown
+++ b/README.markdown
@@ -27,7 +27,7 @@ Wiring up a simple in-memory repository to the transaction manager:
- {@link RepositoryConnectionFactory} handles connections to the corresponding {@link Repository} and manages
+ * the transaction state (represented by {@link SesameTransactionObject}). This class provides methods to access transactional connections from the outside and is typically
+ * the only class that library users interact with. Retrieves the connection for the current transaction. This method may be called at any time as long as a
+ * transaction is active and will return the same connection-handle to the repository in the same
+ * transaction context (which is thread-local). Closes the connection and cleans up the (thread-local) state for the current transaction. This method should not be called manually, since the connection is managed by the
+ * {@link SesameTransactionManager}. Shuts down the {@link Repository} if it was initialized before. Creates a new {@link SesameTransactionObject}, connects the created object
+ * to the corresponding {@link Repository} and disables auto-commit on the connection. This method should only be called by {@link SesameTransactionManager}. Ends the active transaction by either rolling-back or committing the changes to the {@link Repository}
+ * depending on the rollback-flag. This method should only be called by {@link SesameTransactionManager}. Retrieves the current transaction state. This method should only be called by {@link SesameTransactionManager}. {@link RepositoryManagerConnectionFactory} handles connections to a {@link RepositoryManager} and manages
+ * the transaction state (represented by {@link SesameTransactionObject}). The {@link RepositoryManager}'s repository-id is set via the property
+ * {@link RepositoryManagerConnectionFactory#setLocalRepositoryId(String)}/
+ * {@link org.openrdf.spring.RepositoryManagerConnectionFactory#getLocalRepositoryId()}.
+ *
+ *
+ */
+ @Override
+ public RepositoryConnection getConnection() {
+ SesameTransactionObject sesameTransactionObject = localTransactionObject.get();
+
+ if (sesameTransactionObject == null) {
+ throw new SesameTransactionException("No transaction active");
+ }
+
+ RepositoryConnection repositoryConnection = sesameTransactionObject.getRepositoryConnection();
+
+ try {
+ if (!repositoryConnection.isOpen()) {
+ throw new SesameTransactionException("Connection closed during transaction");
+ }
+ } catch (RepositoryException e) {
+ throw new SesameTransactionException(e);
+ }
+
+ return repositoryConnection;
+ }
+
+ /**
+ *
+ *
+ */
+ @Override
+ public void closeConnection() {
+ SesameTransactionObject sesameTransactionObject = null;
+ RepositoryConnection repositoryConnection = null;
+
+ try {
+ sesameTransactionObject = localTransactionObject.get();
+
+ if (sesameTransactionObject == null) {
+ throw new SesameTransactionException("No transaction active");
+ }
+
+ repositoryConnection = sesameTransactionObject.getRepositoryConnection();
+
+ try {
+ if (!repositoryConnection.isOpen()) {
+ throw new SesameTransactionException("Connection closed during transaction");
+ }
+ } catch (RepositoryException e) {
+ throw new SesameTransactionException(e);
+ }
+ } finally {
+ if (sesameTransactionObject != null && repositoryConnection != null) {
+ try {
+ repositoryConnection.close();
+ } catch (RepositoryException e) {
+ throw new SesameTransactionException(e);
+ }
+
+ localTransactionObject.remove();
+ }
+ }
+ }
+
+ /**
+ * true
the current transaction is rolled back, if false
the pending
+ * changes on the connection are committed to the {@link Repository}.
+ * @throws RepositoryException if
+ *
+ *
+ * @throws SesameTransactionException if
+ *
+ *
+ */
+ @Override
+ public void endTransaction(boolean rollback) throws RepositoryException {
+ SesameTransactionObject sesameTransactionObject = localTransactionObject.get();
+
+ if (sesameTransactionObject == null) {
+ throw new TransactionSystemException("No transaction active");
+ }
+
+ RepositoryConnection repositoryConnection = sesameTransactionObject.getRepositoryConnection();
+
+ if (!repositoryConnection.isOpen()) {
+ throw new SesameTransactionException("Connection closed during transaction");
+ }
+
+ if (rollback) {
+ repositoryConnection.rollback();
+ } else {
+ repositoryConnection.commit();
+ }
+ }
+
+ /**
+ *
This class provides methods to access transactional connections from the outside and is typically + * the only class that library users interact with.
+ * + * @author ameingast@gmail.com + */ +public class RepositoryManagerConnectionFactory implements SesameConnectionFactory { + private final RepositoryManager repositoryManager; + + private ThreadLocal{@link SesameConnectionFactory} handles connections to the corresponding {@link Repository} and manages - * the transaction state (represented by {@link SesameTransactionObject}).
- * - *This class provides methods to access transactional connections from the outside and is typically - * the only class that library users interact with.
- * - * @author ameingast@gmail.com - */ -public class SesameConnectionFactory implements DisposableBean { - private final ThreadLocalRetrieves the connection for the current transaction. This method may be called at any time as long as a - * transaction is active and will return the same connection-handle to the repository in the same - * transaction context (which is thread-local).
- * - * @return the {@link RepositoryConnection} - * @throws SesameTransactionException if - *Closes the connection and cleans up the (thread-local) state for the current transaction.
- * - *This method should not be called manually, since the connection is managed by the - * {@link SesameTransactionManager}.
- * - * - * @throws SesameTransactionException if - *Shuts down the {@link Repository} if it was initialized before.
- * - * @throws Exception {@see Repository#shutDown} - */ - @Override - public void destroy() throws Exception { - if (repository != null && repository.isInitialized()) { - try { - repository.shutDown(); - } finally { - repository = null; - } - } - } - - /** - *Creates a new {@link SesameTransactionObject}, connects the created object - * to the corresponding {@link Repository} and disables auto-commit on the connection.
- * - *This method should only be called by {@link SesameTransactionManager}.
- * - * @return the created transaction object representing the transaction state. - * @throws RepositoryException {@see Repository#getConnection} - */ - SesameTransactionObject createTransaction() throws RepositoryException { - RepositoryConnection connection = repository.getConnection(); - connection.setAutoCommit(false); - - SesameTransactionObject sesameTransactionObject = new SesameTransactionObject(connection); - localTransactionObject.set(sesameTransactionObject); - - return sesameTransactionObject; - } - - /** - *Ends the active transaction by either rolling-back or committing the changes to the {@link Repository} - * depending on the rollback-flag.
- * - *This method should only be called by {@link SesameTransactionManager}.
- * - * @param rollback iftrue
the current transaction is rolled back, if false
the pending
- * changes on the connection are committed to the {@link Repository}.
- * @throws RepositoryException if
- * Retrieves the current transaction state.
- * - *This method should only be called by {@link SesameTransactionManager}.
- * - * @return The current transaction state in form of a {@link SesameTransactionObject}. - */ - SesameTransactionObject getLocalTransactionObject() { - return localTransactionObject.get(); - } - - @Override - public String toString() { - return "SesameConnectionFactory{" + - "repository=" + repository + - ", localTransactionObject=" + localTransactionObject + - '}'; - } + SesameTransactionObject getLocalTransactionObject(); } diff --git a/src/test/java/org/openrdf/spring/SesameTransactionTest.java b/src/test/java/org/openrdf/spring/RepositoryManagerTest.java similarity index 81% rename from src/test/java/org/openrdf/spring/SesameTransactionTest.java rename to src/test/java/org/openrdf/spring/RepositoryManagerTest.java index fa907a0..d51fb09 100644 --- a/src/test/java/org/openrdf/spring/SesameTransactionTest.java +++ b/src/test/java/org/openrdf/spring/RepositoryManagerTest.java @@ -21,26 +21,26 @@ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/applicationContext.xml") -public class SesameTransactionTest { +public class RepositoryManagerTest { @Autowired - protected SesameConnectionFactory sesameConnectionFactory; + protected SesameConnectionFactory repositoryConnectionFactory; @Test(expected = SesameTransactionException.class) public void testFactoryDoesNotCreateConnection() throws RepositoryException { - sesameConnectionFactory.getConnection(); + repositoryConnectionFactory.getConnection(); } @Test @Transactional public void testTransactionCreatesConnection() throws RepositoryException { - RepositoryConnection currentConnection = sesameConnectionFactory.getConnection(); + RepositoryConnection currentConnection = repositoryConnectionFactory.getConnection(); Assert.assertNotNull(currentConnection); } @Test @Transactional public void testTransactionDisablesAutoCommit() throws RepositoryException { - RepositoryConnection connection = sesameConnectionFactory.getConnection(); + RepositoryConnection connection = repositoryConnectionFactory.getConnection(); Assert.assertFalse(connection.isAutoCommit()); } @@ -48,7 +48,7 @@ public void testTransactionDisablesAutoCommit() throws RepositoryException { @Test @Transactional public void testWrapTransactions() { - RepositoryConnection connection = sesameConnectionFactory.getConnection(); + RepositoryConnection connection = repositoryConnectionFactory.getConnection(); for (RepositoryConnection repositoryConnection : Arrays.asList(transactionScope(), transactionScopeWithoutAnnotation())) { Assert.assertTrue(connection == repositoryConnection); @@ -57,11 +57,11 @@ public void testWrapTransactions() { @Transactional protected RepositoryConnection transactionScope() { - return sesameConnectionFactory.getConnection(); + return repositoryConnectionFactory.getConnection(); } protected RepositoryConnection transactionScopeWithoutAnnotation() { - return sesameConnectionFactory.getConnection(); + return repositoryConnectionFactory.getConnection(); } @Test @@ -72,7 +72,7 @@ public void testWriteData() throws Exception { } protected void assertDataPresent() throws Exception { - RepositoryConnection connection = sesameConnectionFactory.getConnection(); + RepositoryConnection connection = repositoryConnectionFactory.getConnection(); final TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT ?s ?p ?o WHERE { ?s ?p ?o . }"); TupleQueryResult result = tupleQuery.evaluate(); @@ -96,7 +96,7 @@ protected void addData() throws RepositoryException { URI b = f.createURI("http://example.com/b"); URI c = f.createURI("http://example.com/c"); - RepositoryConnection connection = sesameConnectionFactory.getConnection(); + RepositoryConnection connection = repositoryConnectionFactory.getConnection(); connection.add(a, b, c); } } diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml index 2f0fdde..88432a7 100644 --- a/src/test/resources/applicationContext.xml +++ b/src/test/resources/applicationContext.xml @@ -22,11 +22,19 @@