DataSourceDeployer extends Deployer implements ShutdownHook, DataSource { public Connection getConnection() throws SQLException { return getConnection(user, pass); }  void setTranscationManagerName(String transcationManagerName); void setName(String name); void setDriver(String driver); void setUrl(String url); void setUser(String user); void setPass(String pass); void setNewConnectionSql(String newConnectionSql); void deploy(); void shutdown(); Connection getConnection(); Connection getConnection(String username, String password); PrintWriter getLogWriter(); int getLoginTimeout(); void setLogWriter(PrintWriter out); void setLoginTimeout(int seconds); boolean isWrapperFor(Class<?> iface); T unwrap(Class<T> iface); Logger getParentLogger();  }
@Test public void testConnection() throws Exception { InitialContext ic = new InitialContext(); TransactionManager tm = (TransactionManager) ic .lookup("TransactionManager"); DataSource ds = (DataSource) ic.lookup("TestDS"); assertNotNull(ds); Connection conn = null; tm.begin(); try { conn = (Connection) ds.getConnection(); assertNotNull(conn); Connection conn1 = ds.getConnection(); ds = (DataSource) ic.lookup("TestDS"); Connection conn2 = ds.getConnection(); assertEquals(conn, conn1); assertEquals(conn, conn2); Transaction tx = tm.suspend(); tm.begin(); conn2 = ds.getConnection(); assertFalse(conn.equals(conn2)); tm.commit(); tm.resume(tx); ds = (DataSource) ic.lookup("TestDS"); conn1 = ds.getConnection(); ds = (DataSource) ic.lookup("OtherTestDS"); conn2 = ds.getConnection(); assertEquals(conn, conn1); assertFalse(conn.equals(conn2)); } finally { if (conn != null) { conn.close(); } } tm.commit(); }
@Test public void testNewConnectionSql() throws Exception { InitialContext ic = new InitialContext(); TransactionManager tm = (TransactionManager) ic .lookup("TransactionManager"); DataSource ds = (DataSource) ic.lookup("TestDS"); assertNotNull(ds); Connection conn = null; java.sql.Statement st = null; java.sql.ResultSet rs = null; tm.begin(); try { conn = (Connection) ds.getConnection(); st = conn.createStatement(); rs = st.executeQuery("select count(0) from testTable"); assertTrue(rs.next()); assertTrue(rs.getInt(1) > 0); } finally { if (rs != null) { rs.close(); } if (st != null) { st.close(); } if (conn != null) { conn.close(); } } tm.commit(); }