org.apache.lucene.store.jdbc.datasource
Class TransactionAwareDataSourceProxy

java.lang.Object
  extended by org.apache.lucene.store.jdbc.datasource.TransactionAwareDataSourceProxy
All Implemented Interfaces:
DataSource

public class TransactionAwareDataSourceProxy
extends Object
implements DataSource

Proxy for a target DataSource, adding awareness of local managed transactions. Similar to a transactional JNDI DataSource as provided by a J2EE server.

Encapsulates both a simple transaction manager based on ThreadLocal and a DataSource that supports it. Should be used when no tranasction managers are used (like JTA or Spring) in order to get simpler support for transactions.

It is by no means aimed at replacing the usage of a proper transaction manager, but is provided for a simple implementation of transactions for JdbcDirectory (resulting in better performance), and integration with an existing DataSource code.

Wraps the created Jdbc Connection with a ConnectionProxy, which will only close the target connection if it is controlled by it.

The most outer Connection within the context of a thread, is the controlling connection. Each inner Connection that will be retrieved using this data source will return the same connection, and each call to close the connection on inner connection will be disregarded. Commiting a connection should be done only on the outer most connection.

A set of simple utilities are provided in the DataSourceUtils for simpler management of the DataSource, and special care is taken if the DataSource uses the ConnectionProxy (such is the case with this data soruce). For example, the DataSourceUtils.commitConnectionIfPossible(java.sql.Connection) and DataSourceUtils.rollbackConnectionIfPossible(java.sql.Connection) will only call commit/rollback if the Connection was created by this data source (otherwise, in a managed environment, it will be called on the actual transaction managed, or it will be using AOP).

Note, that all the code that interacts with the database within the Jdbc Store package does not commit / rollbacks the connection. It only executes it's statements, and if something goes wrong throws an exception. The responsiblity for transaction management is with the calling code, and the TransactionAwareDataSourceProxy is there to help non managed transaction management.

Author:
kimchy
See Also:
DataSourceUtils, DirectoryTemplate

Constructor Summary
TransactionAwareDataSourceProxy(DataSource dataSource)
          Create the data source with the given data source to wrap.
 
Method Summary
 Connection getConnection()
          Creates or returns an alreay created connection.
 Connection getConnection(String username, String password)
          Not supported.
 int getLoginTimeout()
           
 PrintWriter getLogWriter()
           
 DataSource getTargetDataSource()
          Returns the targe data source.
protected  Connection getTransactionAwareConnectionProxy(Connection target, DataSource dataSource, boolean controllsConnection)
          A simple helper that return the Jdbc Connection wrapped in our proxy.
 boolean isWrapperFor(Class<?> iface)
           
 void setLoginTimeout(int seconds)
           
 void setLogWriter(PrintWriter out)
           
<T> T
unwrap(Class<T> iface)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TransactionAwareDataSourceProxy

public TransactionAwareDataSourceProxy(DataSource dataSource)
Create the data source with the given data source to wrap.

Method Detail

getTargetDataSource

public DataSource getTargetDataSource()
Returns the targe data source.


getLoginTimeout

public int getLoginTimeout()
                    throws SQLException
Specified by:
getLoginTimeout in interface DataSource
Throws:
SQLException

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws SQLException
Specified by:
setLoginTimeout in interface DataSource
Throws:
SQLException

getLogWriter

public PrintWriter getLogWriter()
                         throws SQLException
Specified by:
getLogWriter in interface DataSource
Throws:
SQLException

setLogWriter

public void setLogWriter(PrintWriter out)
                  throws SQLException
Specified by:
setLogWriter in interface DataSource
Throws:
SQLException

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException
Not supported.

Specified by:
getConnection in interface DataSource
Throws:
SQLException

getConnection

public Connection getConnection()
                         throws SQLException
Creates or returns an alreay created connection.

If a connection was already created within the context of the local thread, and the close method was not called yet, the ori connection will be returned (and will be a "not controlled" connection, which means that any call to close will be a no op).

Consider using DataSourceUtils.getConnection(javax.sql.DataSource) and DataSourceUtils.releaseConnection(java.sql.Connection) for simpler usage.

Specified by:
getConnection in interface DataSource
Throws:
SQLException

getTransactionAwareConnectionProxy

protected Connection getTransactionAwareConnectionProxy(Connection target,
                                                        DataSource dataSource,
                                                        boolean controllsConnection)
A simple helper that return the Jdbc Connection wrapped in our proxy.


unwrap

public <T> T unwrap(Class<T> iface)
         throws SQLException
Throws:
SQLException

isWrapperFor

public boolean isWrapperFor(Class<?> iface)
                     throws SQLException
Throws:
SQLException


Copyright (c) 2004-2008 The Compass Project.