Search in sources :

Example 11 with ConnectionAction

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

the class DalTransactionManagerTest method testDoInTransaction.

@Test
public void testDoInTransaction() {
    final DalHints hints = new DalHints();
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        assertFalse(DalTransactionManager.isInTransaction());
        ConnectionAction<?> action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                assertNotNull(DalTransactionManager.getCurrentDbMeta());
                return null;
            }
        };
        action.operation = DalEventEnum.EXECUTE;
        test.doInTransaction(action, hints);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
    assertFalse(DalTransactionManager.isInTransaction());
    assertNull(DalTransactionManager.getCurrentDbMeta());
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalTransactionManager(com.ctrip.platform.dal.dao.client.DalTransactionManager) ConnectionAction(com.ctrip.platform.dal.dao.client.ConnectionAction) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 12 with ConnectionAction

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

the class DalConnectionManagerTest method testDoInConnection.

@Test
public void testDoInConnection() {
    final boolean useMaster = true;
    final DalHints hints = new DalHints();
    try {
        final DalConnectionManager test = getDalConnectionManager(noShardDb);
        ConnectionAction<Object> action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                connHolder = test.getNewConnection(hints, useMaster, DalEventEnum.BATCH_CALL);
                statement = connHolder.getConn().createStatement();
                rs = statement.executeQuery("select * from " + SqlServerTestInitializer.TABLE_NAME);
                rs.next();
                return null;
            }
        };
        action.operation = DalEventEnum.EXECUTE;
        test.doInConnection(action, hints);
        assertTrue(action.conn == null);
        assertTrue(action.statement == null);
        assertTrue(action.rs == null);
        assertTrue(action.connHolder == null);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalConnectionManager(com.ctrip.platform.dal.dao.client.DalConnectionManager) ConnectionAction(com.ctrip.platform.dal.dao.client.ConnectionAction) Test(org.junit.Test)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)12 ConnectionAction (com.ctrip.platform.dal.dao.client.ConnectionAction)12 Test (org.junit.Test)12 DalTransactionManager (com.ctrip.platform.dal.dao.client.DalTransactionManager)11 SQLException (java.sql.SQLException)11 DalTransactionListener (com.ctrip.platform.dal.dao.client.DalTransactionListener)4 DalClient (com.ctrip.platform.dal.dao.DalClient)1 DalCommand (com.ctrip.platform.dal.dao.DalCommand)1 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)1 DalConnectionManager (com.ctrip.platform.dal.dao.client.DalConnectionManager)1 ResultSet (java.sql.ResultSet)1