1 package org.hibernate.test.connections; 3 4 import org.hibernate.cfg.Configuration; 5 import org.hibernate.cfg.Environment; 6 import org.hibernate.ConnectionReleaseMode; 7 import org.hibernate.Session; 8 import org.hibernate.tool.hbm2ddl.SchemaExport; 9 import org.hibernate.connection.ConnectionProvider; 10 import org.hibernate.connection.ConnectionProviderFactory; 11 import org.hibernate.connection.UserSuppliedConnectionProvider; 12 13 import java.sql.Connection ; 14 import java.sql.ResultSet ; 15 16 import junit.framework.Test; 17 import junit.framework.TestSuite; 18 19 24 public class SuppliedConnectionTest extends ConnectionManagementTestCase { 25 26 private ConnectionProvider cp = ConnectionProviderFactory.newConnectionProvider(); 27 private Connection connectionUnderTest; 28 29 public SuppliedConnectionTest(String name) { 30 super( name ); 31 } 32 33 public static Test suite() { 34 return new TestSuite( SuppliedConnectionTest.class ); 35 } 36 37 protected Session getSessionUnderTest() throws Throwable { 38 connectionUnderTest = cp.getConnection(); 39 return getSessions().openSession( connectionUnderTest ); 40 } 41 42 protected void reconnect(Session session) { 43 session.reconnect( connectionUnderTest ); 44 } 45 46 protected void done() throws Throwable { 47 cp.closeConnection( connectionUnderTest ); 48 } 49 50 protected void configure(Configuration cfg) { 51 cfg.setProperty( Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.ON_CLOSE.toString() ); 52 cfg.setProperty( Environment.CONNECTION_PROVIDER, UserSuppliedConnectionProvider.class.getName() ); 53 boolean supportsScroll = true; 54 try { 55 Connection conn = cp.getConnection(); 56 supportsScroll = conn.getMetaData().supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE); 57 } 58 catch( Throwable ignore ) { 59 } 60 cfg.setProperty( Environment.USE_SCROLLABLE_RESULTSET, "" + supportsScroll ); 61 } 62 63 protected boolean dropAfterFailure() { 64 return false; 65 } 66 67 protected boolean recreateSchema() { 68 return false; 69 } 70 71 protected void setUp() throws Exception { 72 super.setUp(); 73 Connection conn = cp.getConnection(); 74 try { 75 new SchemaExport( getCfg(), conn ).create( false, true ); 76 } 77 finally { 78 if ( conn != null ) { 79 try { 80 cp.closeConnection( conn ); 81 } 82 catch( Throwable ignore ) { 83 } 84 } 85 } 86 } 87 88 protected void tearDown() throws Exception { 89 Connection conn = cp.getConnection(); 90 try { 91 new SchemaExport( getCfg(), conn ).drop( false, true ); 92 } 93 finally { 94 if ( conn != null ) { 95 try { 96 cp.closeConnection( conn ); 97 } 98 catch( Throwable ignore ) { 99 } 100 } 101 } 102 try { 103 cp.close(); 104 } 105 catch( Throwable ignore ) { 106 } 107 super.tearDown(); 108 } 109 } 110 | Popular Tags |