Package com.skyline.energy.dataaccess.jdbc

Examples of com.skyline.energy.dataaccess.jdbc.ConnectionHolder


      return dataSource.getConnection();
    }

    boolean shouldAddCount = false;
    Connection con = null;
    ConnectionHolder conHolder = txObject.getConnectionHolder();
    if (conHolder != null && conHolder.getCurrentConnection() != null) {// 已经存在事务
      // 判断是否可以使用上次的connection
      if (dataSource.equals(txObject.getTxDataSource())) {// 只对同一数据源进行事务处理
        con = conHolder.getCurrentConnection();
        shouldAddCount = true;
      } else {
        con = dataSource.getConnection();
      }
    } else { // 第一次开始事务
      JdbcTransactionManager txManager = (JdbcTransactionManager) txObject.getTxManager();
      if (distribute) {// 分布式数据源,只在第一此写操作时启动事务
        if (needBeginTransaction()) {
          beginNewTransaction(dataSource, txObject);
          con = txObject.getConnectionHolder().getCurrentConnection();
          shouldAddCount = true;
        } else {// 创建独立于事务之外的connection
          con = dataSource.getConnection();
        }
      } else if (txManager.isLazyBegin()) {// 仅懒启动式事务,第一次启动事务
        beginNewTransaction(dataSource, txObject);
        con = txObject.getConnectionHolder().getCurrentConnection();
        shouldAddCount = true;
      } else {
        throw new TransactionException("not lazyBegin transaction bu no currentConnection exist");
      }

      conHolder = txObject.getConnectionHolder(); // 重新获取ConnectionHolder
    }

    if (shouldAddCount) {
      conHolder.addCounter();
    }

    return con;
  }
View Full Code Here


  private static void beginNewTransaction(DataSource dataSource, JdbcTransactionContext txObject) {
    Connection con = null;
    try {
      Connection newCon = dataSource.getConnection();
      LOGGER.debug("Acquired Connection [" + newCon + "] for JDBC transaction");
      ConnectionHolder conHolder = new ConnectionHolder(newCon);
      txObject.setConnectionHolder(conHolder);
      txObject.setTxDataSource(dataSource);

      con = conHolder.getCurrentConnection();

      JdbcUtils.prepareConnectionForTransaction(newCon, txObject.getCurrentDefinition());
    } catch (Exception ex) {
      releaseConnection(con);
      throw new TransactionException("Could not open JDBC Connection for transaction", ex);
View Full Code Here

      JdbcTransactionContext txObject = (JdbcTransactionContext) TransactionContextHolder.getContext();
      if (txObject == null) {// 不支持事务
        closeConnection(con);
      }

      ConnectionHolder conHolder = txObject.getConnectionHolder();
      if (conHolder != null && conHolder.getCurrentConnection() != null) {
        Connection conInTx = conHolder.getCurrentConnection();
        if (conInTx.equals(con)) {// 当前连接和事务处于同一事务,不关闭连接
          conHolder.reduceCounter();
          return;
        }
      }

      // 不在同一事务中,直接关闭连接
View Full Code Here

    JdbcTransactionContext txObject = (JdbcTransactionContext) TransactionContextHolder.getContext();
    if (txObject == null) {// 不支持事务
      throw new TransactionException("Transaction not exist.");
    }

    ConnectionHolder conHolder = txObject.getConnectionHolder();
    if (conHolder.isOpen()) {// 还有后续事务,不提交
      return;
    }

    if (con != null) {
      LOGGER.debug("Transaction commit.");
View Full Code Here

TOP

Related Classes of com.skyline.energy.dataaccess.jdbc.ConnectionHolder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.