com.onewebsql.util.transaction
Class TransactionalDataSource

java.lang.Object
  extended by com.onewebsql.util.transaction.TransactionalDataSource
All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource

public class TransactionalDataSource
extends Object
implements DataSource

Thin wrapper on DataSource that provides transaction management functionality at the data source level. It allow OneWebSQL database operations (that operate on data source level) be grouped into separate transactions.

TransactionalDataSource is dedicated to use in standalone application and JEE application server that don't provide Java Transaction API (JTA). Tomcat for example.

In JEE application servers that provide Java Transaction API (JTA) for managing transactions you should use that API instead of TransactionalDataSource.

Below you will find basic example of usage TransactionalDataSource.
 // create and configure your data source (e.g. PostgreSQL data source)
 PGSimpleDataSource ds = new PGSimpleDataSource();
 ds.setServerName("localhost");
 ds.setDatabaseName("transactiondemo");
 ds.setUser("transactiondemo");
 ds.setPassword("transactiondemo");

 // create our transactional data source wrapper
 TransactionalDataSource transactionalDs = new TransactionalDataSource(ds);
 
 // start transaction
 transactionalDs.beginTransaction();
 
 // do operation on database using DAOs
 BookDAO bookDAO = new BookDAOImpl(transactionalDs, new PostgresDBAdapter());
 
 Book book = bookDAO.getByPK(...);
 book.setPrice(12.45);
 bookDAO.update(book);
 ...
 
 // commit transaction
 transactionalDs.commitTransaction();
 
More information about OneWebSQL and transactions you will find in "OneWebSQL Transaction Guide".


Nested Class Summary
static class TransactionalDataSource.ConnectionProxyInvocationHandler
          Invocation handler for database connection proxy returned by TransactionalDataSource.
static class TransactionalDataSource.TransactionStatus
          Transaction status enumeration.
 
Constructor Summary
TransactionalDataSource(DataSource dataSource)
          Creates transactional data source that operates on connection provided by given data source.
 
Method Summary
 void beginTransaction()
          Starts transaction.
 void commitTransaction()
          Commits current transaction.
 Connection getConnection()
          This methods returns proxy to undelying database connection.
 Connection getConnection(String username, String password)
          This methods has no sense in context of managing transactions.
 int getLoginTimeout()
           
 PrintWriter getLogWriter()
           
 int getTransactionIsolation()
          Gets isolation level of current transaction.
 TransactionalDataSource.TransactionStatus getTransactionStatus()
          Returns status of current transaction.
 boolean isWrapperFor(Class<?> iface)
           
 void markTransactionForRollback()
          Marks current transaction for rollback.
 void rollbackTransaction()
          Rollback current transaction.
 void setLoginTimeout(int seconds)
           
 void setLogWriter(PrintWriter out)
           
 void setTransactionIsolation(int level)
          Sets transaction isolation level.
<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

TransactionalDataSource

public TransactionalDataSource(DataSource dataSource)
Creates transactional data source that operates on connection provided by given data source.

Parameters:
dataSource - wrapped data source
Method Detail

getConnection

public Connection getConnection()
                         throws SQLException
This methods returns proxy to undelying database connection.

Specified by:
getConnection in interface DataSource
Throws:
RuntimeTransactionException - if there is no active transaction
SQLException
See Also:
TransactionalDataSource.ConnectionProxyInvocationHandler

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException
This methods has no sense in context of managing transactions. So it always throws exception.

Specified by:
getConnection in interface DataSource
Throws:
RuntimeTransactionException - for all invocations
SQLException

isWrapperFor

public boolean isWrapperFor(Class<?> iface)
                     throws SQLException
Specified by:
isWrapperFor in interface Wrapper
Throws:
SQLException

unwrap

public <T> T unwrap(Class<T> iface)
         throws SQLException
Specified by:
unwrap in interface Wrapper
Throws:
SQLException

getLoginTimeout

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

getLogWriter

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

setLoginTimeout

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

setLogWriter

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

beginTransaction

public void beginTransaction()
Starts transaction.

Throws:
RuntimeTransactionException - if you try start transaction that is already started

commitTransaction

public void commitTransaction()
Commits current transaction. After commit you operate outside transactions. You can start new transaction by invoking beginTransaction(). After commit underlying database connection is closed and transaction status changes to TransactionalDataSource.TransactionStatus.COMMITTED.

Throws:
RuntimeTransactionException - if transaction is not started, transaction is marked for rollback, exception occurs during committing transaction or exception occurs on closing underlying database connection
com.onewebsql.util.exceptions.RuntimeDoubleException - if exception occurs during committing transaction and then on closing underlying database connection

rollbackTransaction

public void rollbackTransaction()
Rollback current transaction. After rollback you operate outside transactions. You can start new transaction by invoking beginTransaction(). After rollback underlying database connection is closed and transaction status changes to TransactionalDataSource.TransactionStatus.ROLLEDBACK.

Throws:
RuntimeTransactionException - if transaction is not started, exception occurs during rolling back transaction or exception occurs on closing underlying database connection
com.onewebsql.util.exceptions.RuntimeDoubleException - if exception occurs during rolling back transaction and then on closing underlying database connection

markTransactionForRollback

public void markTransactionForRollback()
Marks current transaction for rollback.

Throws:
RuntimeTransactionException - if there is no active transaction (TransactionalDataSource.TransactionStatus.ACTIVE)

getTransactionStatus

public TransactionalDataSource.TransactionStatus getTransactionStatus()
Returns status of current transaction.

Returns:
status of current transaction
See Also:
TransactionalDataSource.TransactionStatus

setTransactionIsolation

public void setTransactionIsolation(int level)
Sets transaction isolation level. Transaction isolation level should be set before any database operations. It can be before or just after beginTransaction(). Possibility of setting transaction isolation level during transactions depends on implementation of database engine and JDBC driver. More information about managing transaction isolation level you will find in "OneWebSQL Transaction Guide".

Parameters:
level - transaction isolation level
Throws:
RuntimeTransactionException - if exception occurs on setting isolation level on underlying database connection

getTransactionIsolation

public int getTransactionIsolation()
Gets isolation level of current transaction.



Copyright © 2011-2013 e-point SA. All Rights Reserved.