我是靠谱客的博主 英俊酸奶,最近开发中收集的这篇文章主要介绍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);//设置是否自动提交 }

}

最后

以上就是英俊酸奶为你收集整理的mysql一个连接就是一个事物_在JDBC的事务操作中,必须操作的是同一个Connection连接吗?...的全部内容,希望文章能够帮你解决mysql一个连接就是一个事物_在JDBC的事务操作中,必须操作的是同一个Connection连接吗?...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部