com.jenkov.mrpersister.scope
Class ScopingDataSource

java.lang.Object
  extended by com.jenkov.mrpersister.scope.ScopingDataSource
All Implemented Interfaces:
javax.sql.DataSource

public class ScopingDataSource
extends java.lang.Object
implements javax.sql.DataSource

A DataSource capable of scoping connections and transactions. You can either use instances of this class directly, or use it via a ScopeFactory.

The ScopingDataSource needs a real DataSource implementation to obtain the connections from.

Using the ScopingDataSource directly to demarcate a connection scope can be done like this:

ScopingDataSource scopingDataSource = new ScopingDataSource(dataSource);

try{
   scopingDatasSource.beginConnectionScope();
   Connection connection = scopingDataSource.getConnection();
   //do something with connection
  
   // same connection as previously returned
   Connection connection2 = scopingDataSource.getConnection();
   //do something with the connection
  
   scopingDataSource.endConnectionScope();
} catch(Throwable t){
   scopingDataSource.endConnectionScope(t);
}


Demarcating a transaction scope can be done similarly, like this:
try{
   scopingDatasSource.beginTransactionScope();
   Connection connection = scopingDataSource.getConnection();
   //do something with connection
  
   // same connection as previously returned
   Connection connection2 = scopingDataSource.getConnection();
   //do something with the connection
  
   scopingDataSource.endTransactionScope();
} catch(Throwable t){
   scopingDataSource.abortTransactionScope(t);
}

Author:
Jakob Jenkov - Copyright 2005 Jenkov Development

Field Summary
 java.util.Map connectionScopes
           
protected  javax.sql.DataSource dataSource
           
 java.util.Map transactionScopes
           
 
Constructor Summary
ScopingDataSource(javax.sql.DataSource dataSource)
           
 
Method Summary
 void abortTransactionScope(java.lang.Throwable rootCause)
          Aborts the transaction scope for the thread calling this method.
 void beginConnectionScope()
          Starts a connection scope for the thread calling this method.
 void beginTransactionScope()
          Begins a transaction scope for the thread calling this method.
 void endConnectionScope()
          Ends the connection scope for the tread calling this method.
 void endConnectionScope(java.lang.Throwable error)
          Ends the connection scope for the thread calling this method, and rethrows the given error (Throwable) in a ScopeException.
 void endTransactionScope()
          Ends the transaction scope for the thread calling this method.
 java.sql.Connection getConnection()
          Returns a connection.
 java.sql.Connection getConnection(java.lang.String username, java.lang.String password)
          Returns a connection.
 int getLoginTimeout()
           
 java.io.PrintWriter getLogWriter()
           
 boolean isConnectionOpen()
          Returns true if a connection is currently open inside this connection or transaction scope.
 boolean isInsideConnectionScope()
          Returns true if the thread calling this method is currently inside a connection scope.
 boolean isInsideTransactionScope()
          Returns true if the thread calling this method is currently inside a transaction scope.
 void setLoginTimeout(int seconds)
           
 void setLogWriter(java.io.PrintWriter out)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dataSource

protected javax.sql.DataSource dataSource

connectionScopes

public java.util.Map connectionScopes

transactionScopes

public java.util.Map transactionScopes
Constructor Detail

ScopingDataSource

public ScopingDataSource(javax.sql.DataSource dataSource)
Method Detail

getLoginTimeout

public int getLoginTimeout()
                    throws java.sql.SQLException
Specified by:
getLoginTimeout in interface javax.sql.DataSource
Throws:
java.sql.SQLException

setLoginTimeout

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

getLogWriter

public java.io.PrintWriter getLogWriter()
                                 throws java.sql.SQLException
Specified by:
getLogWriter in interface javax.sql.DataSource
Throws:
java.sql.SQLException

setLogWriter

public void setLogWriter(java.io.PrintWriter out)
                  throws java.sql.SQLException
Specified by:
setLogWriter in interface javax.sql.DataSource
Throws:
java.sql.SQLException

isInsideConnectionScope

public boolean isInsideConnectionScope()
Returns true if the thread calling this method is currently inside a connection scope. False if not.

Returns:
true if the thread calling this method is currently inside a connection scope. False if not.

isConnectionOpen

public boolean isConnectionOpen()
Returns true if a connection is currently open inside this connection or transaction scope.

Returns:

beginConnectionScope

public void beginConnectionScope()
Starts a connection scope for the thread calling this method.


endConnectionScope

public void endConnectionScope()
Ends the connection scope for the tread calling this method. If a connection is open in this connection scope it will be closed.


endConnectionScope

public void endConnectionScope(java.lang.Throwable error)
Ends the connection scope for the thread calling this method, and rethrows the given error (Throwable) in a ScopeException. If a connection is open in this connection scope it will be closed.

Parameters:
error - The error that causes this connection scope to end.

isInsideTransactionScope

public boolean isInsideTransactionScope()
Returns true if the thread calling this method is currently inside a transaction scope. False if not.

Returns:
true if the thread calling this method is currently inside a transaction scope. False if not.

beginTransactionScope

public void beginTransactionScope()
Begins a transaction scope for the thread calling this method.


endTransactionScope

public void endTransactionScope()
Ends the transaction scope for the thread calling this method. If a connection is opened within the transaction scope, the transaction is committed, and the connection closed.


abortTransactionScope

public void abortTransactionScope(java.lang.Throwable rootCause)
Aborts the transaction scope for the thread calling this method. If a transaction is currently running it will be rolled back, and the connection closed. Finally the given rootCause Throwable will be rethrown wrapped in a ScopeException.

Parameters:
rootCause - The exception that is the reason this transaction scope should be aborted.

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException
Returns a connection. If the thread calling this method is currently inside a connection or transaction scope, the connections returned by this method will be the same connection, until the connection or transaction scope ends. In other words, connections are reused inside a connection or transaction scope.

Specified by:
getConnection in interface javax.sql.DataSource
Returns:
A connection.
Throws:
java.sql.SQLException - If a connection cannot be obtained from the internal DataSource.

getConnection

public java.sql.Connection getConnection(java.lang.String username,
                                         java.lang.String password)
                                  throws java.sql.SQLException
Returns a connection. If the thread calling this method is currently inside a connection or transaction scope, the connections returned by this method will be the same connection, until the connection or transaction scope ends. In other words, connections are reused inside a connection or transaction scope.

Specified by:
getConnection in interface javax.sql.DataSource
Parameters:
username - The user name be used when creating the connection initially.
password - The password be used when creating the connection initially.
Returns:
A connection.
Throws:
java.sql.SQLException - If a connection cannot be obtained from the internal DataSource.