Search in sources :

Example 1 with SoftTransactionManager

use of com.dangdang.ddframe.rdb.transaction.soft.api.SoftTransactionManager in project sharding-jdbc by dangdangdotcom.

the class Main method updateFailure.

private static void updateFailure(final DataSource dataSource) throws SQLException {
    String sql1 = "UPDATE t_order SET status='UPDATE_1' WHERE user_id=10 AND order_id=1000";
    String sql2 = "UPDATE t_order SET not_existed_column=1 WHERE user_id=1 AND order_id=?";
    String sql3 = "UPDATE t_order SET status='UPDATE_2' WHERE user_id=10 AND order_id=1000";
    SoftTransactionManager transactionManager = new SoftTransactionManager(getSoftTransactionConfiguration(dataSource));
    transactionManager.init();
    BEDSoftTransaction transaction = (BEDSoftTransaction) transactionManager.getTransaction(SoftTransactionType.BestEffortsDelivery);
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        transaction.begin(conn);
        PreparedStatement preparedStatement1 = conn.prepareStatement(sql1);
        PreparedStatement preparedStatement2 = conn.prepareStatement(sql2);
        preparedStatement2.setObject(1, 1000);
        PreparedStatement preparedStatement3 = conn.prepareStatement(sql3);
        preparedStatement1.executeUpdate();
        preparedStatement2.executeUpdate();
        preparedStatement3.executeUpdate();
    } finally {
        transaction.end();
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) BEDSoftTransaction(com.dangdang.ddframe.rdb.transaction.soft.bed.BEDSoftTransaction) SoftTransactionManager(com.dangdang.ddframe.rdb.transaction.soft.api.SoftTransactionManager)

Example 2 with SoftTransactionManager

use of com.dangdang.ddframe.rdb.transaction.soft.api.SoftTransactionManager in project sharding-jdbc by dangdangdotcom.

the class AbstractSoftTransactionMockTest method setUp.

@Before
public void setUp() throws Exception {
    SoftTransactionConfiguration softTransactionConfiguration = mock(SoftTransactionConfiguration.class);
    when(softTransactionConfiguration.getTransactionLogDataSource()).thenReturn(mock(DataSource.class));
    when(softTransactionConfiguration.getBestEffortsDeliveryJobConfiguration()).thenReturn(Optional.<NestedBestEffortsDeliveryJobConfiguration>absent());
    softTransactionConfiguration.setBestEffortsDeliveryJobConfiguration(Optional.<NestedBestEffortsDeliveryJobConfiguration>absent());
    softTransactionManager = new SoftTransactionManager(softTransactionConfiguration);
}
Also used : SoftTransactionConfiguration(com.dangdang.ddframe.rdb.transaction.soft.api.config.SoftTransactionConfiguration) DataSource(javax.sql.DataSource) SoftTransactionManager(com.dangdang.ddframe.rdb.transaction.soft.api.SoftTransactionManager) Before(org.junit.Before)

Example 3 with SoftTransactionManager

use of com.dangdang.ddframe.rdb.transaction.soft.api.SoftTransactionManager in project sharding-jdbc by dangdangdotcom.

the class SoftTransactionTest method bedSoftTransactionTest.

@Test
public void bedSoftTransactionTest() throws SQLException {
    SoftTransactionManager transactionManagerFactory = new SoftTransactionManager(getSoftTransactionConfiguration(getShardingDataSource()));
    transactionManagerFactory.init();
    BEDSoftTransaction transactionManager = (BEDSoftTransaction) transactionManagerFactory.getTransaction(SoftTransactionType.BestEffortsDelivery);
    transactionManager.begin(getShardingDataSource().getConnection());
    insert();
    assertThat(select(), is(1));
    transactionManager.end();
}
Also used : BEDSoftTransaction(com.dangdang.ddframe.rdb.transaction.soft.bed.BEDSoftTransaction) SoftTransactionManager(com.dangdang.ddframe.rdb.transaction.soft.api.SoftTransactionManager) Test(org.junit.Test) AbstractSoftTransactionIntegrationTest(com.dangdang.ddframe.rdb.transaction.soft.base.AbstractSoftTransactionIntegrationTest)

Aggregations

SoftTransactionManager (com.dangdang.ddframe.rdb.transaction.soft.api.SoftTransactionManager)3 BEDSoftTransaction (com.dangdang.ddframe.rdb.transaction.soft.bed.BEDSoftTransaction)2 SoftTransactionConfiguration (com.dangdang.ddframe.rdb.transaction.soft.api.config.SoftTransactionConfiguration)1 AbstractSoftTransactionIntegrationTest (com.dangdang.ddframe.rdb.transaction.soft.base.AbstractSoftTransactionIntegrationTest)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 DataSource (javax.sql.DataSource)1 Before (org.junit.Before)1 Test (org.junit.Test)1