Пример #1
0
public class TransactionHolder implements TransactionManager {

  private static final Logger LOGGER = Logger.getLogger(TransactionHolder.class);

  private static DataSource dataSource;

  public TransactionHolder(DataSource dataSource) {
    TransactionHolder.dataSource = dataSource;
  }

  private static final ThreadLocal<TransactionManager> tranManager =
      new ThreadLocal<TransactionManager>() {
        protected TransactionManager initialValue() {
          LOGGER.info("TransactionHolder Initialize");
          return new TransactionManagerImpl(dataSource);
        }
      };

  @Override
  public void beginTransaction() {
    tranManager.get().beginTransaction();
  }

  @Override
  public void commit() {
    tranManager.get().commit();
  }

  @Override
  public void rollback() {
    tranManager.get().rollback();
  }

  @Override
  public Connection getConnection() {
    return tranManager.get().getConnection();
  }
}
Пример #2
0
 protected TransactionManager initialValue() {
   LOGGER.info("TransactionHolder Initialize");
   return new TransactionManagerImpl(dataSource);
 }