Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Commit

Permalink
Version aligned to 2.6.10
Browse files Browse the repository at this point in the history
Introduced RepositoryManagerConnectionFactory
  • Loading branch information
ameingast committed Jun 25, 2013
1 parent c4cdf54 commit 3fbf7e2
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 197 deletions.
124 changes: 117 additions & 7 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

[![Build Status](https://api.travis-ci.org/ameingast/sesame-spring.png)](https://travis-ci.org/ameingast/sesame-spring)


Sesame Spring provides Spring integration for the Openrdf/Sesame library.
Sesame Spring provides Spring integration for the OpenRDF/Sesame library.

## Spring Transactions
The library provides a simple PlatformTransactionManager with thread-local scope for Sesame.

## Usage
## Examples

### Creating a transaction manager for a single repository

Wiring up a simple in-memory repository to the transaction manager:
You should use this approach, if you only deal with a single repository.

```xml
<context:annotation-config/>
Expand All @@ -36,8 +37,6 @@ Wiring up a simple in-memory repository to the transaction manager:
</bean>
```

Transactions are now executed like this:

```java
public class TransactionTest {
// Retrieve the connection factory to access the repository
Expand All @@ -50,7 +49,8 @@ public class TransactionTest {
RepositoryConnection connection = sesameConnectionFactory.getConnection();

// Create a tuple query
TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT ?s ?p ?o WHERE { ?s ?p ?o . }");
TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL,
"SELECT ?s ?p ?o WHERE { ?s ?p ?o . }");

// Fetch the result
TupleQueryResult result = tupleQuery.evaluate();
Expand All @@ -62,3 +62,113 @@ public class TransactionTest {

The transaction manager determines whether to roll-back the transaction in case of an exception or simply to commit
the changes and close the connection once finished.


### Creating a transaction manager for a repository handled by a repository manager

If you access repositories through a sesame repository-manager, you can use the RepositoryManagerConnectionFactory
and supply a repository-id either through the Spring configuration, or exchange it at run-time.

```xml
<context:annotation-config/>
<context:component-scan base-package="org.openrdf.spring"/>
<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="sesameConnectionFactory" class="org.openrdf.spring.RepositoryManagerConnectionFactory">
<constructor-arg ref="repositoryManager"/>
<property name="repositoryId" value="test-id"/>
</bean>

<bean id="repositoryManager" class="org.openrdf.repository.manager.LocalRepositoryManager"
init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<bean class="java.io.File">
<constructor-arg value="/tmp"/>
</bean>
</constructor-arg>
</bean>

<bean id="transactionManager" class="org.openrdf.spring.SesameTransactionManager">
<constructor-arg ref="repositoryManagerConnectionFactory"/>
</bean>
```

### Creating many transaction managers for multiple repositories handled by a repository manager

In the same fashion you can define many transaction managers for multiple repositories in the application-context and
access the repository through them:

```xml
<context:annotation-config/>
<context:component-scan base-package="org.openrdf.spring"/>
<tx:annotation-driven transaction-manager="test"/>
<tx:annotation-driven transaction-manager="data"/>

<bean id="repositoryManager" class="org.openrdf.repository.manager.LocalRepositoryManager"
init-method="initialize" destroy-method="shutDown">
<constructor-arg>
<bean class="java.io.File">
<constructor-arg value="/tmp"/>
</bean>
</constructor-arg>
</bean>

<bean name="testConnectionFactory" class="org.openrdf.spring.RepositoryManagerConnectionFactory">
<constructor-arg name="repositoryManager" ref="repositoryManager"/>
<constructor-arg name="repositoryId" value="test"/>
</bean>

<bean name="dataConnectionFactory" class="org.openrdf.spring.RepositoryManagerConnectionFactory">
<constructor-arg name="repositoryManager" ref="repositoryManager"/>
<constructor-arg name="repositoryId" value="data"/>
</bean>

<bean id="test" class="org.openrdf.spring.SesameTransactionManager">
<constructor-arg ref="testConnectionFactory"/>
</bean>

<bean id="data" class="org.openrdf.spring.SesameTransactionManager">
<constructor-arg ref="dataConnectionFactory"/>
</bean>
```

```java
public class TransactionTest {
// Retrieve the connection factory to access the repository
@Autowired
private SesameConnectionFactory testConnectionFactory;

@Autowired
private SesameConnectionFactory dataConnectionFactory;

@Transactional("test")
public void doSomethingTransactionalWithTheTestRepository() throws Exception {
// Acquire the connection
RepositoryConnection connection = testConnectionFactory.getConnection();

// Create a tuple query
TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL,
"SELECT ?s ?p ?o WHERE { ?s ?p ?o . }");

// Fetch the result
TupleQueryResult result = tupleQuery.evaluate();

// work with the result ...
}

@Transactional("data")
public void doSomethingTransactionalWithTheDataRepository() throws Exception {
// Acquire the connection
RepositoryConnection connection = dataConnectionFactory.getConnection();

// Create a tuple query
TupleQuery tupleQuery = connection.prepareTupleQuery(QueryLanguage.SPARQL,
"SELECT ?s ?p ?o WHERE { ?s ?p ?o . }");

// Fetch the result
TupleQueryResult result = tupleQuery.evaluate();

// work with the result ...
}
}
```
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

<groupId>org.openrdf.sesame</groupId>
<artifactId>sesame-spring</artifactId>
<version>1.0</version>
<version>2.6.10</version>
<name>${project.artifactId}</name>

<description>
Spring integration for the Openrdf/Sesame library.
Spring integration for the OpenRDF/Sesame library.
</description>

<developers>
Expand Down Expand Up @@ -140,11 +140,10 @@
</build>

<properties>
<spring.version>3.1.3.RELEASE</spring.version>
<sesame.version>2.6.10</sesame.version>
<spring.version>3.2.3.RELEASE</spring.version>
<sesame.version>${project.version}</sesame.version>
<junit.version>4.10</junit.version>
<slf4j.version>1.7.2</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<cglib.version>2.2.2</cglib.version>
<maven-compiler.version>2.3.2</maven-compiler.version>

Expand Down
96 changes: 27 additions & 69 deletions src/main/java/org/openrdf/spring/RepositoryConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,32 @@
import org.springframework.transaction.TransactionSystemException;

/**
* <p>{@link RepositoryConnectionFactory} handles connections to the corresponding {@link Repository} and manages
* <p>{@link RepositoryConnectionFactory} handles connections to a single corresponding {@link Repository} and manages
* the transaction state (represented by {@link SesameTransactionObject}).</p>
* <p/>
* <p>This class provides methods to access <i>transactional</i> connections from the outside and is typically
* the only class that library users interact with.</p>
* <p>This class provides methods to access <i>transactional</i> connections from the outside and is typically the
* only class that library users interact with.</p>
*
* @author ameingast@gmail.com
* @see SesameConnectionFactory
*/
public class RepositoryConnectionFactory implements DisposableBean, SesameConnectionFactory {
private final ThreadLocal<SesameTransactionObject> localTransactionObject;

private Repository repository;
private final Repository repository;

/**
* <p>Creates a new {@link RepositoryConnectionFactory} for the provided {@link Repository}.</p>
*
* @param repository The repository to which connections are opened.
*/
public RepositoryConnectionFactory(Repository repository) {
this.repository = repository;
localTransactionObject = new ThreadLocal<SesameTransactionObject>();
}

/**
* <p>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).</p>
*
* @return the {@link RepositoryConnection}
* @throws SesameTransactionException if
* <ul>
* <li>No transaction is active</li>
* </ul>
* @inheritDoc
*/
@Override
public RepositoryConnection getConnection() {
Expand All @@ -58,17 +56,7 @@ public RepositoryConnection getConnection() {
}

/**
* <p>Closes the connection and cleans up the (thread-local) state for the current transaction.</p>
* <p/>
* <p>This method should not be called manually, since the connection is managed by the
* {@link SesameTransactionManager}.</p>
* <p></p>
*
* @throws SesameTransactionException if
* <ul>
* <li>No transaction is active</li>
* <li>The connection could not be closed</li>
* </ul>
* @inheritDoc
*/
@Override
public void closeConnection() {
Expand Down Expand Up @@ -105,29 +93,7 @@ public void closeConnection() {
}

/**
* <p>Shuts down the {@link Repository} if it was initialized before.</p>
*
* @throws Exception {@see Repository#shutDown}
*/
@Override
public void destroy() throws Exception {
if (repository != null && repository.isInitialized()) {
try {
repository.shutDown();
} finally {
repository = null;
}
}
}

/**
* <p>Creates a new {@link SesameTransactionObject}, connects the created object
* to the corresponding {@link Repository} and disables auto-commit on the connection.</p>
* <p/>
* <p>This method should only be called by {@link SesameTransactionManager}.</p>
*
* @return the created transaction object representing the transaction state.
* @throws RepositoryException {@see Repository#getConnection}
* @inheritDoc
*/
@Override
public SesameTransactionObject createTransaction() throws RepositoryException {
Expand All @@ -141,23 +107,7 @@ public SesameTransactionObject createTransaction() throws RepositoryException {
}

/**
* <p>Ends the active transaction by either rolling-back or committing the changes to the {@link Repository}
* depending on the rollback-flag.</p>
* <p/>
* <p>This method should only be called by {@link SesameTransactionManager}.</p>
*
* @param rollback if <code>true</code> the current transaction is rolled back, if <code>false</code> the pending
* changes on the connection are committed to the {@link Repository}.
* @throws RepositoryException if
* <ul>
* <li>The changes could not be rolled back</li>
* <li>The changes could not be committed</li>
* </ul>
* @throws SesameTransactionException if
* <ul>
* <li>No transaction is active</li>
* <li>The connection could not be closed</li>
* </ul>
* @inheritDoc
*/
@Override
public void endTransaction(boolean rollback) throws RepositoryException {
Expand All @@ -181,17 +131,25 @@ public void endTransaction(boolean rollback) throws RepositoryException {
}

/**
* <p>Retrieves the current transaction state.</p>
* <p/>
* <p>This method should only be called by {@link SesameTransactionManager}.</p>
*
* @return The current transaction state in form of a {@link SesameTransactionObject}.
* @inheritDoc
*/
@Override
public SesameTransactionObject getLocalTransactionObject() {
return localTransactionObject.get();
}

/**
* <p>Shuts down the associated {@link Repository} if it was initialized before.</p>
*
* @throws Exception {@see Repository#shutDown}
*/
@Override
public void destroy() throws Exception {
if (repository != null && repository.isInitialized()) {
repository.shutDown();
}
}

@Override
public String toString() {
return "RepositoryConnectionFactory{" +
Expand Down
Loading

0 comments on commit 3fbf7e2

Please sign in to comment.