Search in sources :

Example 6 with DalConnection

use of com.ctrip.platform.dal.dao.client.DalConnection in project dal by ctripcorp.

the class DalConnectionTest method testApplyHints.

@Test
public void testApplyHints() throws SQLException {
    Connection conn = null;
    try {
        DalConnection test = getConnection();
        DalHints hints = new DalHints();
        hints.setIsolationLevel(Connection.TRANSACTION_SERIALIZABLE);
        test.applyHints(hints);
        conn = test.getConn();
        assertTrue(conn.getTransactionIsolation() == Connection.TRANSACTION_SERIALIZABLE);
        hints.setIsolationLevel(Connection.TRANSACTION_NONE);
        test.applyHints(hints);
        assertTrue(conn.getTransactionIsolation() == Connection.TRANSACTION_NONE);
    } catch (Throwable e) {
        fail();
        e.printStackTrace();
    } finally {
        if (conn != null)
            conn.close();
    }
}
Also used : DalConnection(com.ctrip.platform.dal.dao.client.DalConnection) DalHints(com.ctrip.platform.dal.dao.DalHints) Connection(java.sql.Connection) DalConnection(com.ctrip.platform.dal.dao.client.DalConnection) Test(org.junit.Test)

Example 7 with DalConnection

use of com.ctrip.platform.dal.dao.client.DalConnection in project dal by ctripcorp.

the class ConnectionActionTest method testCleanupCloseConnection.

@Test
public void testCleanupCloseConnection() {
    SQLException e1 = new SQLException("test discard", "1234");
    e1.setNextException(new SQLException("test discard", "08006"));
    SQLException e2 = new SQLException("test discard", "1234");
    e2.setNextException(new SQLException("test discard", "08S01"));
    Exception[] el = new Exception[] { // Case 1 detect direct SQLException
    new SQLException("test discard", "08006"), // Case 2 detect embedded SQLException wrapped by DalException
    new DalException("test discard", new SQLException("test discard", "08006")), // Case 3 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", new SQLException("test discard", "08006")), // Case 4 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", e1), // Case 1 detect direct SQLException
    new SQLException("test discard", "08006"), // Case 2 detect embedded SQLException wrapped by DalException
    new DalException("test discard", new SQLException("test discard", "08006")), // Case 3 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", new SQLException("test discard", "08006")), // Case 4 detect embedded SQLException wrapped by NullPinterException
    new RuntimeException("test discard", e2) };
    for (Exception e : el) {
        try {
            TestConnectionAction test = new TestConnectionAction();
            DalConnection connHolder = getDalConnection();
            test.connHolder = connHolder;
            test.statement = test.connHolder.getConn().createStatement();
            test.rs = test.statement.executeQuery("select * from " + SqlServerTestInitializer.TABLE_NAME);
            test.rs.next();
            PooledConnection c = (PooledConnection) connHolder.getConn().unwrap(PooledConnection.class);
            connHolder.error(e);
            test.cleanup();
            assertTrue(c.isDiscarded());
            assertTrue(c.isReleased());
            assertNotNull(test);
            assertTrue(test.conn == null);
            assertTrue(test.statement == null);
            assertTrue(test.rs == null);
            assertTrue(test.connHolder == null);
        } catch (Exception ex) {
            ex.printStackTrace();
            fail("There should be no exception here");
        }
    }
}
Also used : DalConnection(com.ctrip.platform.dal.dao.client.DalConnection) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) SQLException(java.sql.SQLException) DalException(com.ctrip.platform.dal.exceptions.DalException) DalException(com.ctrip.platform.dal.exceptions.DalException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 8 with DalConnection

use of com.ctrip.platform.dal.dao.client.DalConnection in project dal by ctripcorp.

the class ConnectionActionTest method testCleanupCloseConnectionNegative.

@Test
public void testCleanupCloseConnectionNegative() {
    try {
        TestConnectionAction test = new TestConnectionAction();
        DalConnection connHolder = getDalConnection();
        test.connHolder = connHolder;
        test.statement = test.connHolder.getConn().createStatement();
        test.rs = test.statement.executeQuery("select * from " + SqlServerTestInitializer.TABLE_NAME);
        test.rs.next();
        PooledConnection c = (PooledConnection) connHolder.getConn().unwrap(PooledConnection.class);
        connHolder.error(new NullPointerException("0800"));
        test.cleanup();
        assertTrue(!c.isDiscarded());
        assertTrue(!c.isReleased());
        assertNotNull(test);
        assertTrue(test.conn == null);
        assertTrue(test.statement == null);
        assertTrue(test.rs == null);
        assertTrue(test.connHolder == null);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("There should be no exception here");
    }
}
Also used : DalConnection(com.ctrip.platform.dal.dao.client.DalConnection) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) DalException(com.ctrip.platform.dal.exceptions.DalException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 9 with DalConnection

use of com.ctrip.platform.dal.dao.client.DalConnection in project dal by ctripcorp.

the class ConnectionActionTest method getDalConnection.

private DalConnection getDalConnection() throws Exception {
    Connection conn = null;
    conn = DalClientFactory.getDalConfigure().getLocator().getConnection(connectionString);
    return new DalConnection(conn, true, null, DbMeta.createIfAbsent(connectionString, DalClientFactory.getDalConfigure().getDatabaseSet(connectionString).getDatabaseCategory(), conn));
}
Also used : DalConnection(com.ctrip.platform.dal.dao.client.DalConnection) Connection(java.sql.Connection) PooledConnection(org.apache.tomcat.jdbc.pool.PooledConnection) DalConnection(com.ctrip.platform.dal.dao.client.DalConnection)

Example 10 with DalConnection

use of com.ctrip.platform.dal.dao.client.DalConnection in project dal by ctripcorp.

the class DalTransactionTest method getDalConnection.

private DalConnection getDalConnection() throws Exception {
    Connection conn = null;
    conn = DalClientFactory.getDalConfigure().getLocator().getConnection(logicDbName);
    return new DalConnection(conn, true, null, DbMeta.createIfAbsent(logicDbName, null, conn));
}
Also used : DalConnection(com.ctrip.platform.dal.dao.client.DalConnection) Connection(java.sql.Connection) DalConnection(com.ctrip.platform.dal.dao.client.DalConnection)

Aggregations

DalConnection (com.ctrip.platform.dal.dao.client.DalConnection)14 Connection (java.sql.Connection)10 Test (org.junit.Test)10 PooledConnection (org.apache.tomcat.jdbc.pool.PooledConnection)3 DalHints (com.ctrip.platform.dal.dao.DalHints)2 DalException (com.ctrip.platform.dal.exceptions.DalException)2 SQLException (java.sql.SQLException)2 DalConnectionManager (com.ctrip.platform.dal.dao.client.DalConnectionManager)1 DbMeta (com.ctrip.platform.dal.dao.client.DbMeta)1 LogEntry (com.ctrip.platform.dal.dao.client.LogEntry)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1