[hibernate-commits] Hibernate SVN: r13928 - in sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc: proxy and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Aug 16 15:02:03 EDT 2007


Author: steve.ebersole at jboss.com
Date: 2007-08-16 15:02:03 -0400 (Thu, 16 Aug 2007)
New Revision: 13928

Added:
   sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/AggressiveReleaseTest.java
Modified:
   sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/ConnectionProviderBuilder.java
   sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java
   sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/TestingServiceImpl.java
Log:
tests of after_statement release

Modified: sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/ConnectionProviderBuilder.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/ConnectionProviderBuilder.java	2007-08-16 12:46:28 UTC (rev 13927)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/ConnectionProviderBuilder.java	2007-08-16 19:02:03 UTC (rev 13928)
@@ -28,11 +28,19 @@
  */
 public class ConnectionProviderBuilder {
 	public static ConnectionProvider buildConnectionProvider() {
+		return buildConnectionProvider( false );
+	}
+
+	public static ConnectionProvider buildConnectionProvider(final boolean allowAggressiveRelease) {
 		Properties props = new Properties( null );
 		props.put( Environment.DRIVER, "org.hsqldb.jdbcDriver" );
 		props.put( Environment.URL, "jdbc:hsqldb:mem:target/test/db/hsqldb/hibernate" );
 		props.put( Environment.USER, "sa" );
-		DriverManagerConnectionProvider connectionProvider = new DriverManagerConnectionProvider();
+		DriverManagerConnectionProvider connectionProvider = new DriverManagerConnectionProvider() {
+			public boolean supportsAggressiveRelease() {
+				return allowAggressiveRelease;
+			}
+		};
 		connectionProvider.configure( props );
 		return connectionProvider;
 	}

Added: sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/AggressiveReleaseTest.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/AggressiveReleaseTest.java	                        (rev 0)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/AggressiveReleaseTest.java	2007-08-16 19:02:03 UTC (rev 13928)
@@ -0,0 +1,268 @@
+/*
+ * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, v. 2.1. This program is distributed in the
+ * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details. You should have received a
+ * copy of the GNU Lesser General Public License, v.2.1 along with this
+ * distribution; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Red Hat Author(s): Steve Ebersole
+ */
+package org.hibernate.jdbc.proxy;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import org.hibernate.ConnectionReleaseMode;
+import org.hibernate.jdbc.impl.ConnectionObserver;
+import org.hibernate.jdbc.impl.LogicalConnectionImpl;
+
+/**
+ * AggressiveReleaseTest implementation
+ *
+ * @author Steve Ebersole
+ */
+public class AggressiveReleaseTest {
+	private static final Logger log = LoggerFactory.getLogger( AggressiveReleaseTest.class );
+	private TestingServiceImpl services = new TestingServiceImpl();
+
+	private static class ConnectionCounter implements ConnectionObserver {
+		public int obtainCount = 0;
+		public int releaseCount = 0;
+
+		public void physicalConnectionObtained(Connection connection) {
+			obtainCount++;
+		}
+
+		public void physicalConnectionReleased() {
+			releaseCount++;
+		}
+
+		public void logicalConnectionClosed() {
+		}
+	}
+
+	@BeforeClass
+	protected void create() {
+		log.debug( "starting @BeforeClass" );
+		services.prepare( true );
+	}
+
+	@AfterClass
+	protected void destroy() {
+		services.release();
+	}
+
+	@BeforeMethod
+	protected void setUpSchema() throws SQLException {
+		log.debug( "starting @BeforeMethod" );
+		Connection connection = null;
+		Statement stmnt = null;
+		try {
+			connection = services.getConnectionProvider().getConnection();
+			stmnt = connection.createStatement();
+			stmnt.execute( "drop table SANDBOX_JDBC_TST if exists" );
+			stmnt.execute( "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" );
+		}
+		finally {
+			if ( stmnt != null ) {
+				try {
+					stmnt.close();
+				}
+				catch ( SQLException ignore ) {
+					log.warn( "could not close statement used to set up schema", ignore );
+				}
+			}
+			if ( connection != null ) {
+				try {
+					connection.close();
+				}
+				catch ( SQLException ignore ) {
+					log.warn( "could not close connection used to set up schema", ignore );
+				}
+			}
+		}
+	}
+
+	@AfterMethod
+	protected void tearDownSchema() throws SQLException {
+		Connection connection = null;
+		Statement stmnt = null;
+		try {
+			connection = services.getConnectionProvider().getConnection();
+			stmnt = connection.createStatement();
+			stmnt.execute( "drop table SANDBOX_JDBC_TST if exists" );
+		}
+		finally {
+			if ( stmnt != null ) {
+				try {
+					stmnt.close();
+				}
+				catch ( SQLException ignore ) {
+					log.warn( "could not close statement used to set up schema", ignore );
+				}
+			}
+			if ( connection != null ) {
+				try {
+					connection.close();
+				}
+				catch ( SQLException ignore ) {
+					log.warn( "could not close connection used to set up schema", ignore );
+				}
+			}
+		}
+	}
+
+	@Test
+	public void testBasicRelease() {
+		LogicalConnectionImpl logicalConnection = new LogicalConnectionImpl( null, ConnectionReleaseMode.AFTER_STATEMENT, services );
+		Connection proxiedConnection = ProxyBuilder.buildConnection( logicalConnection );
+		ConnectionCounter observer = new ConnectionCounter();
+		logicalConnection.addObserver( observer );
+
+		try {
+			PreparedStatement ps = proxiedConnection.prepareStatement( "insert into SANDBOX_JDBC_TST( ID, NAME ) values ( ?, ? )" );
+			ps.setLong( 1, 1 );
+			ps.setString( 2, "name" );
+			ps.execute();
+			assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 1, observer.obtainCount );
+			assertEquals( 0, observer.releaseCount );
+			ps.close();
+			assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 1, observer.obtainCount );
+			assertEquals( 1, observer.releaseCount );
+		}
+		catch ( SQLException sqle ) {
+			fail( "incorrect exception type : sqlexception" );
+		}
+		finally {
+			logicalConnection.close();
+		}
+
+		assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+	}
+
+	@Test
+	public void testReleaseCircumventedByHeldResources() {
+		LogicalConnectionImpl logicalConnection = new LogicalConnectionImpl( null, ConnectionReleaseMode.AFTER_STATEMENT, services );
+		Connection proxiedConnection = ProxyBuilder.buildConnection( logicalConnection );
+		ConnectionCounter observer = new ConnectionCounter();
+		logicalConnection.addObserver( observer );
+
+		try {
+			PreparedStatement ps = proxiedConnection.prepareStatement( "insert into SANDBOX_JDBC_TST( ID, NAME ) values ( ?, ? )" );
+			ps.setLong( 1, 1 );
+			ps.setString( 2, "name" );
+			ps.execute();
+			assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 1, observer.obtainCount );
+			assertEquals( 0, observer.releaseCount );
+			ps.close();
+			assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 1, observer.obtainCount );
+			assertEquals( 1, observer.releaseCount );
+
+			// open a result set and hold it open...
+			ps = proxiedConnection.prepareStatement( "select * from SANDBOX_JDBC_TST" );
+			ps.executeQuery();
+			assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 2, observer.obtainCount );
+			assertEquals( 1, observer.releaseCount );
+
+			// open a second result set
+			PreparedStatement ps2 = proxiedConnection.prepareStatement( "select * from SANDBOX_JDBC_TST" );
+			ps2.execute();
+			assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 2, observer.obtainCount );
+			assertEquals( 1, observer.releaseCount );
+			// and close it...
+			ps2.close();
+			// the release should be circumvented...
+			assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 2, observer.obtainCount );
+			assertEquals( 1, observer.releaseCount );
+
+			// let the close of the logical connection below release all resources (hopefully)...
+		}
+		catch ( SQLException sqle ) {
+			fail( "incorrect exception type : sqlexception" );
+		}
+		finally {
+			logicalConnection.close();
+		}
+
+		assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+		assertEquals( 2, observer.obtainCount );
+		assertEquals( 2, observer.releaseCount );
+	}
+
+	@Test
+	public void testReleaseCircumventedManually() {
+		LogicalConnectionImpl logicalConnection = new LogicalConnectionImpl( null, ConnectionReleaseMode.AFTER_STATEMENT, services );
+		Connection proxiedConnection = ProxyBuilder.buildConnection( logicalConnection );
+		ConnectionCounter observer = new ConnectionCounter();
+		logicalConnection.addObserver( observer );
+
+		try {
+			PreparedStatement ps = proxiedConnection.prepareStatement( "insert into SANDBOX_JDBC_TST( ID, NAME ) values ( ?, ? )" );
+			ps.setLong( 1, 1 );
+			ps.setString( 2, "name" );
+			ps.execute();
+			assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 1, observer.obtainCount );
+			assertEquals( 0, observer.releaseCount );
+			ps.close();
+			assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 1, observer.obtainCount );
+			assertEquals( 1, observer.releaseCount );
+
+			// disable releases...
+			logicalConnection.disableReleases();
+
+			// open a result set...
+			ps = proxiedConnection.prepareStatement( "select * from SANDBOX_JDBC_TST" );
+			ps.executeQuery();
+			assertTrue( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 2, observer.obtainCount );
+			assertEquals( 1, observer.releaseCount );
+			// and close it...
+			ps.close();
+			// the release should be circumvented...
+			assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+			assertEquals( 2, observer.obtainCount );
+			assertEquals( 1, observer.releaseCount );
+
+			// let the close of the logical connection below release all resources (hopefully)...
+		}
+		catch ( SQLException sqle ) {
+			fail( "incorrect exception type : sqlexception" );
+		}
+		finally {
+			logicalConnection.close();
+		}
+
+		assertFalse( logicalConnection.getJdbcContainer().hasRegisteredResources() );
+		assertEquals( 2, observer.obtainCount );
+		assertEquals( 2, observer.releaseCount );
+	}
+}

Modified: sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java	2007-08-16 12:46:28 UTC (rev 13927)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/BasicConnectionProxyTest.java	2007-08-16 19:02:03 UTC (rev 13928)
@@ -47,7 +47,7 @@
 
 	@BeforeClass
 	protected void create() {
-		services.prepare();
+		services.prepare( false );
 	}
 
 	@AfterClass
@@ -56,18 +56,29 @@
 	}
 
 	@Test
+	public void testExceptionHandling() {
+		LogicalConnectionImpl logicalConnection = new LogicalConnectionImpl( null, ConnectionReleaseMode.AFTER_TRANSACTION, services );
+		Connection proxiedConnection = ProxyBuilder.buildConnection( logicalConnection );
+		try {
+			proxiedConnection.prepareStatement( "select count(*) from NON_EXISTENT" ).executeQuery();
+		}
+		catch ( SQLException sqle ) {
+			fail( "incorrect exception type : sqlexception" );
+		}
+		catch ( JDBCException ok ) {
+			// expected outcome
+		}
+		finally {
+			logicalConnection.close();
+		}
+	}
+
+	@Test
 	public void testBasicJdbcUsage() throws JDBCException {
 		LogicalConnectionImpl logicalConnection = new LogicalConnectionImpl( null, ConnectionReleaseMode.AFTER_TRANSACTION, services );
 		Connection proxiedConnection = ProxyBuilder.buildConnection( logicalConnection );
 
 		try {
-			try {
-				proxiedConnection.prepareStatement( "select count(*) from NON_EXISTENT" ).executeQuery();
-			}
-			catch ( JDBCException ok ) {
-				// expected outcome
-			}
-
 			Statement stmnt = proxiedConnection.createStatement();
 			stmnt.execute( "drop table SANDBOX_JDBC_TST if exists" );
 			stmnt.execute( "create table SANDBOX_JDBC_TST ( ID integer, NAME varchar(100) )" );

Modified: sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/TestingServiceImpl.java
===================================================================
--- sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/TestingServiceImpl.java	2007-08-16 12:46:28 UTC (rev 13927)
+++ sandbox/trunk/jdbc-proxy/src/test/java/org/hibernate/jdbc/proxy/TestingServiceImpl.java	2007-08-16 19:02:03 UTC (rev 13928)
@@ -38,8 +38,8 @@
 	private JDBCContainerBuilder jdbcContainerBuilder;
 	private ExceptionHelper exceptionHelper;
 
-	public void prepare() {
-		connectionProvider = ConnectionProviderBuilder.buildConnectionProvider();
+	public void prepare(boolean allowAggressiveRelease) {
+		connectionProvider = ConnectionProviderBuilder.buildConnectionProvider( allowAggressiveRelease );
 		sqlStatementLogger = new SQLStatementLogger( true );
 		exceptionHelper = new ExceptionHelper(
 				new SQLStateConverter(




More information about the hibernate-commits mailing list