Search in sources :

Example 11 with DalClient

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

the class DalDirectClientTestStub method executeTestWithMultipleCommandsNotAllSuccessed.

/**
	 * Test execute multiple commands and not all successes.
	 * If one failed, the after commands will be dropped.
	 * @throws SQLException
	 */
@Test
public void executeTestWithMultipleCommandsNotAllSuccessed() throws SQLException {
    final DalHints hints = new DalHints();
    List<DalCommand> commands = new ArrayList<DalCommand>();
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 1";
            StatementParameters parameters = new StatementParameters();
            client.update(sql, parameters, hints);
            return false;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 100";
            StatementParameters parameters = new StatementParameters();
            return client.update(sql, parameters, hints) == 1;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE id = 3";
            StatementParameters parameters = new StatementParameters();
            return client.update(sql, parameters, hints) == 1;
        }
    });
    client.execute(commands, hints);
    List<ClientTestModel> models = this.queryModelsByIds();
    Assert.assertEquals(2, models.size());
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalHints(com.ctrip.platform.dal.dao.DalHints) DalClient(com.ctrip.platform.dal.dao.DalClient) ClientTestModel(test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 12 with DalClient

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

the class DalConcurrentMysqlTest method testExecute.

@Test
public void testExecute() throws SQLException {
    List<DalCommand> commands = new ArrayList<DalCommand>();
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "INSERT INTO " + TABLE_NAME + "(quantity,type,address)" + " VALUES(10, 1, 'SH INFO')";
            int ret = client.update(sql, new StatementParameters(), new DalHints());
            if (ret > 0) {
                System.out.println("insert success.");
            } else {
                System.out.println("insert failed.");
            }
            return ret > 0;
        }
    });
    commands.add(new DalCommand() {

        @Override
        public boolean execute(DalClient client) throws SQLException {
            String sql = "DELETE FROM " + TABLE_NAME + " WHERE address = '" + "SH INFO'";
            int ret = client.update(sql, new StatementParameters(), new DalHints());
            if (ret > 0) {
                System.out.println("delete success");
            } else {
                System.out.println("delete failed");
            }
            return ret > 0;
        }
    });
    client.execute(commands, new DalHints());
    String sql = "SELECT count(1) from " + TABLE_NAME;
    Number count = (Number) client.query(sql, new StatementParameters(), new DalHints(), new DalScalarExtractor());
    Assert.assertEquals(INSERT_COUNT, count.intValue());
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) DalScalarExtractor(com.ctrip.platform.dal.dao.helper.DalScalarExtractor) Test(org.junit.Test)

Example 13 with DalClient

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

the class DalDirectClient method execute.

@Override
public void execute(final List<DalCommand> commands, final DalHints hints) throws SQLException {
    final DalClient client = this;
    ConnectionAction<?> action = new ConnectionAction<Object>() {

        @Override
        public Object execute() throws Exception {
            for (DalCommand cmd : commands) {
                if (!cmd.execute(client))
                    break;
            }
            return null;
        }
    };
    action.populate(commands);
    doInTransaction(action, hints);
}
Also used : DalCommand(com.ctrip.platform.dal.dao.DalCommand) DalClient(com.ctrip.platform.dal.dao.DalClient)

Example 14 with DalClient

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

the class ManualMarkDownTest method logicDbMarkdownTest.

@Test
public void logicDbMarkdownTest() {
    String logicDb = "dao_test_sqlsvr_tableShard_simple";
    DalStatusManager.getDatabaseSetStatus(logicDb).setMarkdown(true);
    DalClient client = DalClientFactory.getClient(logicDb);
    try {
        client.query("select 1", new StatementParameters(), new DalHints(), new DalScalarExtractor());
        Assert.fail();
    } catch (SQLException e) {
    }
    DalStatusManager.getDatabaseSetStatus(logicDb).setMarkdown(false);
    try {
        client.query("select 1", new StatementParameters(), new DalHints(), new DalScalarExtractor());
    } catch (SQLException e) {
        Assert.fail();
    }
}
Also used : DalClient(com.ctrip.platform.dal.dao.DalClient) DalHints(com.ctrip.platform.dal.dao.DalHints) SQLException(java.sql.SQLException) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) DalScalarExtractor(com.ctrip.platform.dal.dao.helper.DalScalarExtractor) Test(org.junit.Test)

Example 15 with DalClient

use of com.ctrip.platform.dal.dao.DalClient 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

DalClient (com.ctrip.platform.dal.dao.DalClient)15 DalHints (com.ctrip.platform.dal.dao.DalHints)13 SQLException (java.sql.SQLException)12 DalCommand (com.ctrip.platform.dal.dao.DalCommand)11 Test (org.junit.Test)11 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)9 ArrayList (java.util.ArrayList)7 Map (java.util.Map)4 DalScalarExtractor (com.ctrip.platform.dal.dao.helper.DalScalarExtractor)3 HashMap (java.util.HashMap)3 ClientTestModel (test.com.ctrip.platform.dal.dao.unitbase.ClientTestModel)3 ResultSet (java.sql.ResultSet)2 DalQueryDao (com.ctrip.platform.dal.dao.DalQueryDao)1 ConnectionAction (com.ctrip.platform.dal.dao.client.ConnectionAction)1 DalTransactionListener (com.ctrip.platform.dal.dao.client.DalTransactionListener)1 DalTransactionManager (com.ctrip.platform.dal.dao.client.DalTransactionManager)1 DalColumnMapRowMapper (com.ctrip.platform.dal.dao.helper.DalColumnMapRowMapper)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1