/**
* {@link Transaction} that lets the container manage the full lifecycle of the transaction.
* Delays connection retrieval until getConnection() is called.
* Ignores all commit or rollback requests.
* By default, it closes the connection but can be configured not to do it.
* @author Clinton Begin
* @see ManagedTransactionFactory
*/publicclassManagedTransactionimplementsTransaction{privatestaticfinal Log log = LogFactory.getLog(ManagedTransaction.class);private DataSource dataSource;private TransactionIsolationLevel level;private Connection connection;privateboolean closeConnection;publicManagedTransaction(Connection connection,boolean closeConnection){this.connection = connection;this.closeConnection = closeConnection;}publicManagedTransaction(DataSource ds, TransactionIsolationLevel level,boolean closeConnection){this.dataSource = ds;this.level = level;this.closeConnection = closeConnection;}@Overridepublic Connection getConnection()throws SQLException {if(this.connection == null){openConnection();}returnthis.connection;}@Overridepublicvoidcommit()throws SQLException {// Does nothing}@Overridepublicvoidrollback()throws SQLException {// Does nothing}@Overridepublicvoidclose()throws SQLException {if(this.closeConnection &&this.connection != null){if(log.isDebugEnabled()){
log.debug("Closing JDBC Connection ["+this.connection +"]");}this.connection.close();}}protectedvoidopenConnection()throws SQLException {if(log.isDebugEnabled()){
log.debug("Opening JDBC Connection");}this.connection =this.dataSource.getConnection();if(this.level != null){this.connection.setTransactionIsolation(this.level.getLevel());}}@Overridepublic Integer getTimeout()throws SQLException {return null;}}