mysql一个连接就是一个事物_在JDBC的事务操作中,必须操作的是同一个Connection连接吗?...

package org.apache.ibatis.transaction.jdbc;

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.logging.Log;

import org.apache.ibatis.logging.LogFactory;

import org.apache.ibatis.session.TransactionIsolationLevel;

import org.apache.ibatis.transaction.Transaction;

import org.apache.ibatis.transaction.TransactionException;

/*** {@link Transaction} that makes use of the JDBC commit and rollback facilities directly.* It relies on the connection retrieved from the dataSource to manage the scope* of the transaction.* Delays connection retrieval until getConnection() is called.* Ignores commit or rollback requests when autocommit is on.** @see JdbcTransactionFactory*/

/*** Jdbc事务* @author Clinton Begin*/

public class JdbcTransaction implements Transaction {

private static final Log log = LogFactory.getLog(JdbcTransaction.class);

protected Connection connection;//数据库连接 protected DataSource dataSource;//数据源 protected TransactionIsolationLevel level;//事务隔离级别 protected boolean autoCommmit;//是否自动提交

public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {

dataSource = ds;

level = desiredLevel;

autoCommmit = desiredAutoCommit;

}

public JdbcTransaction(Connection connection) {

this.connection = connection;

}

@Override

public Connection getConnection() throws SQLException {

if (connection == null) {

openConnection();

}

return connection;

}

@Override

public void commit() throws SQLException {

if (connection != null && !connection.getAutoCommit()) { //连接非空且事务不是自动提交时 if (log.isDebugEnabled()) {

log.debug("Committing JDBC Connection [" + connection + "]");

}

connection.commit();

}

}

@Override

public void rollback() throws SQLException {

if (connection != null && !connection.getAutoCommit()) { //连接非空且事务不是自动提交时 if (log.isDebugEnabled()) {

log.debug("Rolling back JDBC Connection [" + connection + "]");

}

connection.rollback();

}

}

@Override

public void close() throws SQLException {

if (connection != null) {

resetAutoCommit();//重置自动提交为true if (log.isDebugEnabled()) {

log.debug("Closing JDBC Connection [" + connection + "]");

}

connection.close();

}

}

/*** @description 设置是否自动提交事务* @author xudj* @date 2016年7月15日 下午5:37:37* @param desiredAutoCommit*/

protected void setDesiredAutoCommit(boolean desiredAutoCommit) {

try {

//和之前的不相等时进行设置 if (connection.getAutoCommit() != desiredAutoCommit) {

if (log.isDebugEnabled()) {

log.debug("Setting autocommit to " + desiredAutoCommit + " on JDBC Connection [" + connection + "]");

}

connection.setAutoCommit(desiredAutoCommit);

}

} catch (SQLException e) {

// Only a very poorly implemented driver would fail here, // and there's not much we can do about that. throw new TransactionException("Error configuring AutoCommit. "

+ "Your driver may not support getAutoCommit() or setAutoCommit(). "

+ "Requested setting: " + desiredAutoCommit + ". Cause: " + e, e);

}

}

/*** @description 重置事务自动提交* @author xudj* @date 2016年7月15日 下午5:41:40*/

protected void resetAutoCommit() {

try {

if (!connection.getAutoCommit()) {

// MyBatis does not call commit/rollback on a connection if just selects were performed. // Some databases start transactions with select statements // and they mandate a commit/rollback before closing the connection. // A workaround is setting the autocommit to true before closing the connection. // Sybase throws an exception here. if (log.isDebugEnabled()) {

log.debug("Resetting autocommit to true on JDBC Connection [" + connection + "]");

}

connection.setAutoCommit(true);

}

} catch (SQLException e) {

if (log.isDebugEnabled()) {

log.debug("Error resetting autocommit to true "

+ "before closing the connection. Cause: " + e);

}

}

}

/*** @description 打开数据库连接* @author xudj* @date 2016年7月15日 下午1:55:38* @throws SQLException*/

protected void openConnection() throws SQLException {

if (log.isDebugEnabled()) {//先判断是否是debug模式,其它地方作用相同 log.debug("Opening JDBC Connection");

}

connection = dataSource.getConnection();//获取数据库连接 if (level != null) {

//设置事务隔离级别,通过枚举进行获取 connection.setTransactionIsolation(level.getLevel());

}

setDesiredAutoCommit(autoCommmit);//设置是否自动提交 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值