|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.jenkov.mrpersister.scope.ScopingDataSource
public class ScopingDataSource
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);
}
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 |
---|
protected javax.sql.DataSource dataSource
public java.util.Map connectionScopes
public java.util.Map transactionScopes
Constructor Detail |
---|
public ScopingDataSource(javax.sql.DataSource dataSource)
Method Detail |
---|
public int getLoginTimeout() throws java.sql.SQLException
getLoginTimeout
in interface javax.sql.DataSource
java.sql.SQLException
public void setLoginTimeout(int seconds) throws java.sql.SQLException
setLoginTimeout
in interface javax.sql.DataSource
java.sql.SQLException
public java.io.PrintWriter getLogWriter() throws java.sql.SQLException
getLogWriter
in interface javax.sql.DataSource
java.sql.SQLException
public void setLogWriter(java.io.PrintWriter out) throws java.sql.SQLException
setLogWriter
in interface javax.sql.DataSource
java.sql.SQLException
public boolean isInsideConnectionScope()
public boolean isConnectionOpen()
public void beginConnectionScope()
public void endConnectionScope()
public void endConnectionScope(java.lang.Throwable error)
error
- The error that causes this connection scope to end.public boolean isInsideTransactionScope()
public void beginTransactionScope()
public void endTransactionScope()
public void abortTransactionScope(java.lang.Throwable rootCause)
rootCause
- The exception that is the reason this transaction scope
should be aborted.public java.sql.Connection getConnection() throws java.sql.SQLException
getConnection
in interface javax.sql.DataSource
java.sql.SQLException
- If a connection cannot be obtained from the internal DataSource.public java.sql.Connection getConnection(java.lang.String username, java.lang.String password) throws java.sql.SQLException
getConnection
in interface javax.sql.DataSource
username
- The user name be used when creating the connection initially.password
- The password be used when creating the connection initially.
java.sql.SQLException
- If a connection cannot be obtained from the internal DataSource.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |