Search in sources :

Example 1 with BranchTransactionDO

use of io.seata.core.store.BranchTransactionDO in project seata by seata.

the class LogStoreDataBaseDAOTest method updateBranchTransactionDO.

@Test
public void updateBranchTransactionDO() throws SQLException {
    BranchTransactionDO branchTransactionDO = new BranchTransactionDO();
    branchTransactionDO.setResourceId("qqqq");
    branchTransactionDO.setXid("abc-123:8888");
    branchTransactionDO.setTransactionId(1285343);
    branchTransactionDO.setBranchId(343434318);
    branchTransactionDO.setBranchType("TCC");
    branchTransactionDO.setResourceGroupId("abc");
    branchTransactionDO.setResourceGroupId("a");
    branchTransactionDO.setClientId("1.1.1.1");
    branchTransactionDO.setStatus(1);
    branchTransactionDO.setApplicationData("abc=123");
    branchTransactionDO.setResourceGroupId("test");
    boolean ret = logStoreDataBaseDAO.insertBranchTransactionDO(branchTransactionDO);
    Assertions.assertTrue(ret);
    branchTransactionDO.setStatus(3);
    Assertions.assertTrue(logStoreDataBaseDAO.updateBranchTransactionDO(branchTransactionDO));
    String sql = "select * from branch_table where xid= 'abc-123:8888' and branch_id = 343434318";
    String delSql = "delete from branch_table where xid= 'abc-123:8888' and branch_id = 343434318";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sql);
        if (rs.next()) {
            Assertions.assertTrue(true);
            Assertions.assertEquals(3, rs.getInt("status"));
        } else {
            Assertions.assertTrue(false);
        }
        conn.createStatement().execute(delSql);
    } finally {
        IOUtil.close(conn);
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) BranchTransactionDO(io.seata.core.store.BranchTransactionDO) Test(org.junit.jupiter.api.Test)

Example 2 with BranchTransactionDO

use of io.seata.core.store.BranchTransactionDO in project seata by seata.

the class LogStoreDataBaseDAOTest method queryBranchTransactionDO.

@Test
public void queryBranchTransactionDO() throws SQLException {
    {
        // creata data for test
        BranchTransactionDO branchTransactionDO = new BranchTransactionDO();
        branchTransactionDO.setResourceId("qqqq");
        branchTransactionDO.setXid("abc-123:6789");
        branchTransactionDO.setTransactionId(24234235);
        branchTransactionDO.setBranchId(345465676);
        branchTransactionDO.setBranchType("TCC");
        branchTransactionDO.setResourceGroupId("abc");
        branchTransactionDO.setResourceGroupId("a");
        branchTransactionDO.setClientId("1.1.1.1");
        branchTransactionDO.setStatus(1);
        branchTransactionDO.setApplicationData("abc=123");
        branchTransactionDO.setResourceGroupId("test");
        boolean ret = logStoreDataBaseDAO.insertBranchTransactionDO(branchTransactionDO);
        Assertions.assertTrue(ret);
    }
    {
        // creata data for test
        BranchTransactionDO branchTransactionDO = new BranchTransactionDO();
        branchTransactionDO.setResourceId("qqqq");
        branchTransactionDO.setXid("abc-123:6789");
        branchTransactionDO.setTransactionId(24234235);
        branchTransactionDO.setBranchId(78563453);
        branchTransactionDO.setBranchType("TCC");
        branchTransactionDO.setResourceGroupId("abc");
        branchTransactionDO.setResourceGroupId("a");
        branchTransactionDO.setClientId("1.1.1.1");
        branchTransactionDO.setStatus(1);
        branchTransactionDO.setApplicationData("abc=123");
        branchTransactionDO.setResourceGroupId("test");
        boolean ret = logStoreDataBaseDAO.insertBranchTransactionDO(branchTransactionDO);
        Assertions.assertTrue(ret);
    }
    List<BranchTransactionDO> rets = logStoreDataBaseDAO.queryBranchTransactionDO("abc-123:6789");
    Assertions.assertTrue(CollectionUtils.isNotEmpty(rets));
    Assertions.assertEquals(2, rets.size());
    if (78563453 == rets.get(0).getBranchId() && 345465676 == rets.get(1).getBranchId()) {
        Assertions.assertTrue(true);
    } else if (78563453 == rets.get(1).getBranchId() && 345465676 == rets.get(0).getBranchId()) {
        Assertions.assertTrue(true);
    } else {
        Assertions.assertTrue(false);
    }
    String delSql = "delete from branch_table where xid= 'abc-123:6789' ";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        conn.createStatement().execute(delSql);
    } finally {
        IOUtil.close(conn);
    }
}
Also used : Connection(java.sql.Connection) BranchTransactionDO(io.seata.core.store.BranchTransactionDO) Test(org.junit.jupiter.api.Test)

Example 3 with BranchTransactionDO

use of io.seata.core.store.BranchTransactionDO in project seata by seata.

the class LogStoreDataBaseDAOTest method insertBranchTransactionDO.

@Test
public void insertBranchTransactionDO() throws SQLException {
    BranchTransactionDO branchTransactionDO = new BranchTransactionDO();
    branchTransactionDO.setResourceId("qqqq");
    branchTransactionDO.setXid("abc-123:7777");
    branchTransactionDO.setTransactionId(1285343);
    branchTransactionDO.setBranchId(1234508);
    branchTransactionDO.setBranchType("TCC");
    branchTransactionDO.setResourceGroupId("abc");
    branchTransactionDO.setResourceGroupId("a");
    branchTransactionDO.setClientId("1.1.1.1");
    branchTransactionDO.setStatus(1);
    branchTransactionDO.setApplicationData("abc=123");
    branchTransactionDO.setResourceGroupId("test");
    boolean ret = logStoreDataBaseDAO.insertBranchTransactionDO(branchTransactionDO);
    Assertions.assertTrue(ret);
    String sql = "select * from branch_table where xid= 'abc-123:7777' and branch_id = 1234508";
    String delSql = "delete from branch_table where xid= 'abc-123:7777' and branch_id = 1234508";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sql);
        if (rs.next()) {
            Assertions.assertTrue(true);
        } else {
            Assertions.assertTrue(false);
        }
        conn.createStatement().execute(delSql);
    } finally {
        IOUtil.close(conn);
    }
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) BranchTransactionDO(io.seata.core.store.BranchTransactionDO) Test(org.junit.jupiter.api.Test)

Example 4 with BranchTransactionDO

use of io.seata.core.store.BranchTransactionDO in project seata by seata.

the class RedisTransactionStoreManager method readSessionByTransactionId.

/**
 * read the global session by transactionId
 * @param transactionId the transaction id
 * @param withBranchSessions if read branch sessions
 * @return the global session
 */
private GlobalSession readSessionByTransactionId(String transactionId, boolean withBranchSessions) {
    String globalKey = buildGlobalKeyByTransactionId(transactionId);
    String xid = null;
    try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
        Map<String, String> map = jedis.hgetAll(globalKey);
        if (CollectionUtils.isEmpty(map)) {
            return null;
        }
        GlobalTransactionDO globalTransactionDO = (GlobalTransactionDO) BeanUtils.mapToObject(map, GlobalTransactionDO.class);
        if (globalTransactionDO != null) {
            xid = globalTransactionDO.getXid();
        }
        List<BranchTransactionDO> branchTransactionDOs = new ArrayList<>();
        if (withBranchSessions) {
            branchTransactionDOs = this.readBranchSessionByXid(jedis, xid);
        }
        return getGlobalSession(globalTransactionDO, branchTransactionDOs);
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) GlobalTransactionDO(io.seata.core.store.GlobalTransactionDO) ArrayList(java.util.ArrayList) BranchTransactionDO(io.seata.core.store.BranchTransactionDO)

Example 5 with BranchTransactionDO

use of io.seata.core.store.BranchTransactionDO in project seata by seata.

the class RedisTransactionStoreManager method readSession.

/**
 * Read session global session.
 *
 * @param xid the xid
 * @param withBranchSessions  the withBranchSessions
 * @return the global session
 */
@Override
public GlobalSession readSession(String xid, boolean withBranchSessions) {
    String transactionId = String.valueOf(XID.getTransactionId(xid));
    String globalKey = buildGlobalKeyByTransactionId(transactionId);
    try (Jedis jedis = JedisPooledFactory.getJedisInstance()) {
        Map<String, String> map = jedis.hgetAll(globalKey);
        if (CollectionUtils.isEmpty(map)) {
            return null;
        }
        GlobalTransactionDO globalTransactionDO = (GlobalTransactionDO) BeanUtils.mapToObject(map, GlobalTransactionDO.class);
        List<BranchTransactionDO> branchTransactionDOs = null;
        if (withBranchSessions) {
            branchTransactionDOs = this.readBranchSessionByXid(jedis, xid);
        }
        return getGlobalSession(globalTransactionDO, branchTransactionDOs);
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) GlobalTransactionDO(io.seata.core.store.GlobalTransactionDO) BranchTransactionDO(io.seata.core.store.BranchTransactionDO)

Aggregations

BranchTransactionDO (io.seata.core.store.BranchTransactionDO)25 GlobalTransactionDO (io.seata.core.store.GlobalTransactionDO)8 Test (org.junit.jupiter.api.Test)7 Connection (java.sql.Connection)6 ArrayList (java.util.ArrayList)6 BranchSession (io.seata.server.session.BranchSession)5 ResultSet (java.sql.ResultSet)5 DataAccessException (io.seata.common.exception.DataAccessException)4 Jedis (redis.clients.jedis.Jedis)4 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 Pipeline (redis.clients.jedis.Pipeline)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1