⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbctransaction.java

📁 用Java实现的23个常用设计模式源代码
💻 JAVA
字号:
//$Id: JDBCTransaction.java,v 1.6.2.4 2003/11/21 13:17:28 oneovthafew Exp $package net.sf.hibernate.transaction;import java.sql.SQLException;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import net.sf.hibernate.FlushMode;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Transaction;import net.sf.hibernate.TransactionException;import net.sf.hibernate.engine.SessionImplementor;/** * Implements a basic transaction strategy for JDBC connections.This is the * default <tt>Transaction</tt> implementation used if none is explicitly * specified. * @author Anton van Straaten, Gavin King */public class JDBCTransaction implements Transaction {	private SessionImplementor session;	private boolean toggleAutoCommit;	private boolean rolledBack;	private boolean committed;	private boolean begun;	private boolean commitFailed;		private static final Log log = LogFactory.getLog(JDBCTransaction.class);		public JDBCTransaction(SessionImplementor session) throws HibernateException {		this.session = session;	}		public void begin() throws HibernateException {				log.debug("begin");				try {			toggleAutoCommit = session.connection().getAutoCommit();			if ( log.isDebugEnabled() ) log.debug("current autocommit status:" + toggleAutoCommit);			if (toggleAutoCommit) {				log.debug("disabling autocommit");				session.connection().setAutoCommit(false);			}		}		catch (SQLException e) {			log.error("Begin failed", e);			throw new TransactionException("Begin failed with SQL exception: ", e);		}				begun = true;	}		public void commit() throws HibernateException {				if (!begun) throw new TransactionException("Transaction not successfully started");				log.debug("commit");				if ( session.getFlushMode()!=FlushMode.NEVER ) session.flush();		try {			session.connection().commit();			committed = true;			session.afterTransactionCompletion(true);		}		catch (SQLException e) {			log.error("Commit failed", e);			session.afterTransactionCompletion(false);			commitFailed = true;			throw new TransactionException("Commit failed with SQL exception: ", e);		}		finally {			toggleAutoCommit();		}	}		public void rollback() throws HibernateException {				if (!begun) throw new TransactionException("Transaction not successfully started");				log.debug("rollback");				if (!commitFailed) {			try {				session.connection().rollback();				rolledBack = true;			}			catch (SQLException e) {				log.error("Rollback failed", e);				throw new TransactionException("Rollback failed with SQL exception: ", e);			}			finally {				session.afterTransactionCompletion(false);				toggleAutoCommit();			}		}	}		private void toggleAutoCommit() {		try {			if (toggleAutoCommit) {				log.debug("re-enabling autocommit");				session.connection().setAutoCommit(true);			}		}		catch (Exception sqle) {			log.error("Could not toggle autocommit", sqle);			//swallow it (the transaction _was_ successful or successfully rolled back)		}	}		public boolean wasRolledBack() {		return rolledBack;	}	public boolean wasCommitted() {		return committed;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -