Permalink
Browse files

HHH-7398 -- Added testTimeout() and modified AbstractBatchImpl.buildB…

…atchStatement() accordingly.
  • Loading branch information...
pf-john-hardin authored and sebersole committed Aug 29, 2012
1 parent 8b632c2 commit 7e0559ecdbfbb985b412ac5d4ea367f2e8995226
@@ -135,19 +135,7 @@ public PreparedStatement getBatchStatement(String sql, boolean callable) {
}
private PreparedStatement buildBatchStatement(String sql, boolean callable) {
- sql = jdbcCoordinator.getTransactionCoordinator().getTransactionContext().onPrepareStatement( sql );
- try {
- if ( callable ) {
- return jdbcCoordinator.getLogicalConnection().getShareableConnectionProxy().prepareCall( sql );
- }
- else {
- return jdbcCoordinator.getLogicalConnection().getShareableConnectionProxy().prepareStatement( sql );
- }
- }
- catch ( SQLException sqle ) {
- LOG.sqlExceptionEscapedProxy( sqle );
- throw sqlExceptionHelper().convert( sqle, "could not prepare batch statement", sql );
- }
+ return jdbcCoordinator.getStatementPreparer().prepareStatement( sql, callable );
}
@Override
@@ -34,6 +34,7 @@
import org.hibernate.Interceptor;
import org.hibernate.Session;
import org.hibernate.Transaction;
+import org.hibernate.TransactionException;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.type.Type;
@@ -43,6 +44,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
/**
* @author Gavin King
@@ -132,6 +134,40 @@ public boolean onFlushDirty(Object entity, Serializable id, Object[] currentStat
}
+ /**
+ * Test that setting a transaction timeout will cause an Exception to occur
+ * if the transaction timeout is exceeded.
+ */
+ @Test
+ public void testTimeout() throws Exception {
+ final int TIMEOUT = 2;
+ final int WAIT = TIMEOUT + 1;
+ Session s = openSession();
+ // Get the transaction and set the timeout BEFORE calling begin()
+ Transaction t = s.getTransaction();
+ t.setTimeout( TIMEOUT );
+ t.begin();
+ // Sleep for an amount of time that exceeds the transaction timeout
+ Thread.sleep( WAIT * 1000 );
+ try {
+ // Do something with the transaction and try to commit it
+ s.persist( new User( "john", "test" ) );
+ t.commit();
+ fail( "Transaction should have timed out" );
+ }
+ catch ( TransactionException e ) {
+ // Insure that the Exception is "transaction timeout expired"
+ String exceptionActual = e.toString();
+ String exceptionExpected = "org.hibernate.TransactionException: transaction timeout expired";
+ if ( !exceptionActual.contains( exceptionExpected ) ) {
+ String msg = String.format( "Transaction failed for the wrong reason. Expected [%s] but received [%s]",
+ exceptionExpected, exceptionActual );
+ fail( msg );
+
+ }
+ }
+ }
+
@Test
public void testComponentInterceptor() {
final int checkPerm = 500;

0 comments on commit 7e0559e

Please sign in to comment.