Search in sources :

Example 1 with DalTransactionListener

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

the class DalTransactionManagerTest method testRegister.

@Test
public void testRegister() {
    final DalHints hints = new DalHints();
    final DalTransactionListener testListener = new DalTransactionListener() {

        @Override
        public void beforeCommit() {
        }

        @Override
        public void beforeRollback() {
        }

        @Override
        public void afterCommit() {
        }

        @Override
        public void afterRollback() {
        }
    };
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        DalTransactionManager.register(testListener);
        fail();
    } catch (Exception e) {
    }
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        ConnectionAction<?> action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                DalTransactionManager.register(testListener);
                return null;
            }
        };
        action.operation = DalEventEnum.EXECUTE;
        test.doInTransaction(action, hints);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalTransactionListener(com.ctrip.platform.dal.dao.client.DalTransactionListener) 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 2 with DalTransactionListener

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

the class DalTransactionManagerTest method testRollbackListeners.

@Test
public void testRollbackListeners() {
    final DalHints hints = new DalHints();
    final DalTransactionListener testListener = new DalTransactionListener() {

        @Override
        public void beforeCommit() {
        }

        @Override
        public void beforeRollback() {
            Assert.assertTrue(DalTransactionManager.isInTransaction());
        }

        @Override
        public void afterCommit() {
            fail();
        }

        @Override
        public void afterRollback() {
            Assert.assertFalse(DalTransactionManager.isInTransaction());
        }
    };
    final DalTransactionListener testListener1 = new DalTransactionListener() {

        @Override
        public void beforeCommit() throws SQLException {
            throw new SQLException();
        }

        @Override
        public void beforeRollback() {
            Assert.assertTrue(DalTransactionManager.isInTransaction());
        }

        @Override
        public void afterCommit() {
            fail();
        }

        @Override
        public void afterRollback() {
            Assert.assertFalse(DalTransactionManager.isInTransaction());
        }
    };
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        ConnectionAction<?> action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                DalTransactionManager.register(testListener);
                // The 2nd listener will cause transaction rollback
                DalTransactionManager.register(testListener1);
                return null;
            }
        };
        action.operation = DalEventEnum.EXECUTE;
        test.doInTransaction(action, hints);
        fail();
    } catch (Exception e) {
    }
}
Also used : DalTransactionListener(com.ctrip.platform.dal.dao.client.DalTransactionListener) DalHints(com.ctrip.platform.dal.dao.DalHints) DalTransactionManager(com.ctrip.platform.dal.dao.client.DalTransactionManager) SQLException(java.sql.SQLException) ConnectionAction(com.ctrip.platform.dal.dao.client.ConnectionAction) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 3 with DalTransactionListener

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

the class DalTransactionManagerTest method testGetCurrentListeners.

@Test
public void testGetCurrentListeners() {
    final DalHints hints = new DalHints();
    final DalTransactionListener testListener = new DalTransactionListener() {

        @Override
        public void beforeCommit() {
        }

        @Override
        public void beforeRollback() {
        }

        @Override
        public void afterCommit() {
        }

        @Override
        public void afterRollback() {
        }
    };
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        DalTransactionManager.getCurrentListeners();
        fail();
    } catch (Exception e) {
    }
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        ConnectionAction<?> action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                DalTransactionManager.register(testListener);
                DalTransactionManager.register(testListener);
                Assert.assertEquals(2, DalTransactionManager.getCurrentListeners().size());
                return null;
            }
        };
        action.operation = DalEventEnum.EXECUTE;
        test.doInTransaction(action, hints);
        try {
            DalTransactionManager.getCurrentListeners();
            fail();
        } catch (Exception e) {
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalTransactionListener(com.ctrip.platform.dal.dao.client.DalTransactionListener) 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 4 with DalTransactionListener

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

the class DalTransactionManagerTest method testCommitListeners.

@Test
public void testCommitListeners() {
    final DalHints hints = new DalHints();
    final DalTransactionListener testListener = new DalTransactionListener() {

        @Override
        public void beforeCommit() throws SQLException {
            Assert.assertTrue(DalTransactionManager.isInTransaction());
            DalClient c = DalClientFactory.getClient(DalTransactionManager.getLogicDbName());
            c.query("SELECT 1", new StatementParameters(), new DalHints(), new DalResultSetExtractor<Object>() {

                @Override
                public Object extract(ResultSet rs) throws SQLException {
                    return null;
                }
            });
        }

        @Override
        public void beforeRollback() {
            fail();
        }

        @Override
        public void afterCommit() {
            Assert.assertFalse(DalTransactionManager.isInTransaction());
        }

        @Override
        public void afterRollback() {
            fail();
        }
    };
    final DalTransactionListener testListener1 = new DalTransactionListener() {

        @Override
        public void beforeCommit() throws SQLException {
            Assert.assertTrue(DalTransactionManager.isInTransaction());
            DalCommand cmd = new DalCommand() {

                @Override
                public boolean execute(DalClient client) throws SQLException {
                    client.query("SELECT 1", new StatementParameters(), new DalHints(), new DalResultSetExtractor<Object>() {

                        @Override
                        public Object extract(ResultSet rs) throws SQLException {
                            return null;
                        }
                    });
                    return false;
                }
            };
            DalClientFactory.getClient(DalTransactionManager.getLogicDbName()).execute(cmd, new DalHints());
        }

        @Override
        public void beforeRollback() {
            fail();
        }

        @Override
        public void afterCommit() {
            Assert.assertFalse(DalTransactionManager.isInTransaction());
        }

        @Override
        public void afterRollback() {
            fail();
        }
    };
    try {
        final DalTransactionManager test = new DalTransactionManager(getDalConnectionManager());
        ConnectionAction<?> action = new ConnectionAction<Object>() {

            public Object execute() throws Exception {
                DalTransactionManager.register(testListener);
                DalTransactionManager.register(testListener1);
                return null;
            }
        };
        action.operation = DalEventEnum.EXECUTE;
        test.doInTransaction(action, hints);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : DalHints(com.ctrip.platform.dal.dao.DalHints) DalClient(com.ctrip.platform.dal.dao.DalClient) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) SQLException(java.sql.SQLException) DalTransactionListener(com.ctrip.platform.dal.dao.client.DalTransactionListener) DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalTransactionManager(com.ctrip.platform.dal.dao.client.DalTransactionManager) ResultSet(java.sql.ResultSet) ConnectionAction(com.ctrip.platform.dal.dao.client.ConnectionAction) Test(org.junit.Test)

Aggregations

DalHints (com.ctrip.platform.dal.dao.DalHints)4 ConnectionAction (com.ctrip.platform.dal.dao.client.ConnectionAction)4 DalTransactionListener (com.ctrip.platform.dal.dao.client.DalTransactionListener)4 DalTransactionManager (com.ctrip.platform.dal.dao.client.DalTransactionManager)4 SQLException (java.sql.SQLException)4 Test (org.junit.Test)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 ResultSet (java.sql.ResultSet)1