Author: steve.ebersole(a)jboss.com
Date: 2009-11-11 00:58:42 -0500 (Wed, 11 Nov 2009)
New Revision: 17955
Modified:
core/trunk/core/src/main/java/org/hibernate/engine/transaction/Isolater.java
core/trunk/core/src/main/java/org/hibernate/jdbc/Batcher.java
Log:
HHH-4561 - Deprecate openConnection()/closeConnection() methods on Batcher interface
Modified: core/trunk/core/src/main/java/org/hibernate/engine/transaction/Isolater.java
===================================================================
---
core/trunk/core/src/main/java/org/hibernate/engine/transaction/Isolater.java 2009-11-11
05:58:21 UTC (rev 17954)
+++
core/trunk/core/src/main/java/org/hibernate/engine/transaction/Isolater.java 2009-11-11
05:58:42 UTC (rev 17955)
@@ -28,12 +28,15 @@
import java.sql.SQLException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
+import javax.transaction.SystemException;
+import javax.transaction.NotSupportedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.exception.JDBCExceptionHelper;
+import org.hibernate.exception.SQLExceptionConverter;
/**
* Class which provides the isolation semantics required by
@@ -108,80 +111,116 @@
public void delegateWork(IsolatedWork work, boolean transacted) throws
HibernateException {
TransactionManager transactionManager = session.getFactory().getTransactionManager();
- Transaction surroundingTransaction = null;
- Connection connection = null;
- boolean caughtException = false;
try {
- // First we need to suspend any current JTA transaction and obtain
- // a JDBC connection
- surroundingTransaction = transactionManager.suspend();
+ // First we suspend any current JTA transaction
+ Transaction surroundingTransaction = transactionManager.suspend();
if ( log.isDebugEnabled() ) {
log.debug( "surrounding JTA transaction suspended [" +
surroundingTransaction + "]" );
}
- if ( transacted ) {
- transactionManager.begin();
+ boolean hadProblems = false;
+ try {
+ // then peform the requested work
+ if ( transacted ) {
+ doTheWorkInNewTransaction( work, transactionManager );
+ }
+ else {
+ doTheWorkInNoTransaction( work );
+ }
}
+ catch ( HibernateException e ) {
+ hadProblems = true;
+ throw e;
+ }
+ finally {
+ try {
+ transactionManager.resume( surroundingTransaction );
+ if ( log.isDebugEnabled() ) {
+ log.debug( "surrounding JTA transaction resumed [" +
surroundingTransaction + "]" );
+ }
+ }
+ catch( Throwable t ) {
+ // if the actually work had an error use that, otherwise error based on t
+ if ( !hadProblems ) {
+ //noinspection ThrowFromFinallyBlock
+ throw new HibernateException( "Unable to resume previously suspended
transaction", t );
+ }
+ }
+ }
+ }
+ catch ( SystemException e ) {
+ throw new HibernateException( "Unable to suspend current JTA transaction",
e );
+ }
+ }
- connection = session.getBatcher().openConnection();
+ private void doTheWorkInNewTransaction(IsolatedWork work, TransactionManager
transactionManager) {
+ try {
+ // start the new isolated transaction
+ transactionManager.begin();
- // doAfterTransactionCompletion the actual work
- work.doWork( connection );
-
- // if everything went ok, commit the transaction and close the obtained
- // connection handle...
- session.getBatcher().closeConnection( connection );
-
- if ( transacted ) {
+ try {
+ doTheWork( work );
+ // if everythign went ok, commit the isolated transaction
transactionManager.commit();
}
- }
- catch( Throwable t ) {
- // at some point the processing went bad, so we need to:
- // 1) make sure the connection handle gets released
- // 2) try to cleanup the JTA context as much as possible
- caughtException = true;
- try {
- if ( connection != null && !connection.isClosed() ) {
- session.getBatcher().closeConnection( connection );
- }
- }
- catch( Throwable ignore ) {
- log.trace( "unable to release connection on exception [" + ignore +
"]" );
- }
- if ( transacted ) {
+ catch ( Exception e ) {
try {
transactionManager.rollback();
}
- catch( Throwable ignore ) {
- log.trace( "unable to rollback new transaction on exception [" + ignore +
"]" );
+ catch ( Exception ignore ) {
+ log.info( "Unable to rollback isolated transaction on error [" + e +
"] : [" + ignore + "]" );
}
}
- // finally handle the exception
- if ( t instanceof HibernateException ) {
- throw ( HibernateException ) t;
+ }
+ catch ( SystemException e ) {
+ throw new HibernateException( "Unable to start isolated transaction", e );
+ }
+ catch ( NotSupportedException e ) {
+ throw new HibernateException( "Unable to start isolated transaction", e );
+ }
+ }
+
+ private void doTheWorkInNoTransaction(IsolatedWork work) {
+ doTheWork( work );
+ }
+
+ private void doTheWork(IsolatedWork work) {
+ try {
+ // obtain our isolated connection
+ Connection connection =
session.getFactory().getConnectionProvider().getConnection();
+ try {
+ // do the actual work
+ work.doWork( connection );
}
- else {
- throw new HibernateException( "error performing isolated work", t );
+ catch ( HibernateException e ) {
+ throw e;
}
- }
- finally {
- if ( surroundingTransaction != null ) {
+ catch ( Exception e ) {
+ throw new HibernateException( "Unable to perform isolated work", e );
+ }
+ finally {
try {
- transactionManager.resume( surroundingTransaction );
- if ( log.isDebugEnabled() ) {
- log.debug( "surrounding JTA transaction resumed [" +
surroundingTransaction + "]" );
- }
+ // no matter what, release the connection (handle)
+ session.getFactory().getConnectionProvider().closeConnection( connection );
}
- catch( Throwable t ) {
- if ( !caughtException ) {
- throw new HibernateException( "unable to resume previously suspended
transaction", t );
- }
+ catch ( Throwable ignore ) {
+ log.info( "Unable to release isolated connection [" + ignore +
"]" );
}
}
}
+ catch ( SQLException sqle ) {
+ throw JDBCExceptionHelper.convert(
+ sqlExceptionConverter(),
+ sqle,
+ "unable to obtain isolated JDBC connection"
+ );
+ }
}
+
+ private SQLExceptionConverter sqlExceptionConverter() {
+ return session.getFactory().getSQLExceptionConverter();
+ }
}
/**
@@ -196,61 +235,75 @@
}
public void delegateWork(IsolatedWork work, boolean transacted) throws
HibernateException {
- Connection connection = null;
boolean wasAutoCommit = false;
try {
- connection = session.getBatcher().openConnection();
-
- if ( transacted ) {
- if ( connection.getAutoCommit() ) {
- wasAutoCommit = true;
- connection.setAutoCommit( false );
+ Connection connection =
session.getFactory().getConnectionProvider().getConnection();
+ try {
+ if ( transacted ) {
+ if ( connection.getAutoCommit() ) {
+ wasAutoCommit = true;
+ connection.setAutoCommit( false );
+ }
}
- }
- work.doWork( connection );
+ work.doWork( connection );
- if ( transacted ) {
- connection.commit();
+ if ( transacted ) {
+ connection.commit();
+ }
}
- }
- catch( Throwable t ) {
- try {
- if ( transacted && connection != null && !connection.isClosed() ) {
- connection.rollback();
+ catch( Exception e ) {
+ try {
+ if ( transacted && !connection.isClosed() ) {
+ connection.rollback();
+ }
}
- }
- catch( Throwable ignore ) {
- log.trace( "unable to release connection on exception [" + ignore +
"]" );
- }
+ catch( Exception ignore ) {
+ log.info( "unable to rollback connection on exception [" + ignore +
"]" );
+ }
- if ( t instanceof HibernateException ) {
- throw ( HibernateException ) t;
+ if ( e instanceof HibernateException ) {
+ throw ( HibernateException ) e;
+ }
+ else if ( e instanceof SQLException ) {
+ throw JDBCExceptionHelper.convert(
+ sqlExceptionConverter(),
+ ( SQLException ) e,
+ "error performing isolated work"
+ );
+ }
+ else {
+ throw new HibernateException( "error performing isolated work", e );
+ }
}
- else if ( t instanceof SQLException ) {
- throw JDBCExceptionHelper.convert(
- session.getFactory().getSQLExceptionConverter(),
- ( SQLException ) t,
- "error performing isolated work"
- );
- }
- else {
- throw new HibernateException( "error performing isolated work", t );
- }
- }
- finally {
- if ( connection != null ) {
+ finally {
if ( transacted && wasAutoCommit ) {
try {
connection.setAutoCommit( true );
}
- catch( Throwable ignore ) {
+ catch( Exception ignore ) {
log.trace( "was unable to reset connection back to auto-commit" );
}
}
- session.getBatcher().closeConnection( connection );
+ try {
+ session.getFactory().getConnectionProvider().closeConnection( connection );
+ }
+ catch ( Exception ignore ) {
+ log.info( "Unable to release isolated connection [" + ignore +
"]" );
+ }
}
}
+ catch ( SQLException sqle ) {
+ throw JDBCExceptionHelper.convert(
+ sqlExceptionConverter(),
+ sqle,
+ "unable to obtain isolated JDBC connection"
+ );
+ }
}
+
+ private SQLExceptionConverter sqlExceptionConverter() {
+ return session.getFactory().getSQLExceptionConverter();
+ }
}
}
Modified: core/trunk/core/src/main/java/org/hibernate/jdbc/Batcher.java
===================================================================
--- core/trunk/core/src/main/java/org/hibernate/jdbc/Batcher.java 2009-11-11 05:58:21 UTC
(rev 17954)
+++ core/trunk/core/src/main/java/org/hibernate/jdbc/Batcher.java 2009-11-11 05:58:42 UTC
(rev 17955)
@@ -165,10 +165,14 @@
/**
* Obtain a JDBC connection
+ *
+ * @deprecated Obtain connections from {@link ConnectionProvider} instead
*/
public Connection openConnection() throws HibernateException;
/**
* Dispose of the JDBC connection
+ *
+ * @deprecated Obtain connections from {@link ConnectionProvider} instead
*/
public void closeConnection(Connection conn) throws HibernateException;