Search in sources :

Example 1 with GlobalTransactionDO

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

the class LogStoreDataBaseDAOTest method queryGlobalTransactionDO_by_transaction_id.

@Test
public void queryGlobalTransactionDO_by_transaction_id() throws SQLException {
    GlobalTransactionDO globalTransactionDO = new GlobalTransactionDO();
    globalTransactionDO.setXid("abc-123:676787978");
    globalTransactionDO.setApplicationData("abc=234356");
    globalTransactionDO.setTransactionServiceGroup("abc");
    globalTransactionDO.setTransactionName("test");
    globalTransactionDO.setTransactionId(867978970);
    globalTransactionDO.setTimeout(20);
    globalTransactionDO.setBeginTime(System.currentTimeMillis());
    globalTransactionDO.setApplicationId("test");
    globalTransactionDO.setStatus(1);
    boolean ret = logStoreDataBaseDAO.insertGlobalTransactionDO(globalTransactionDO);
    Assertions.assertTrue(ret);
    GlobalTransactionDO globalTransactionDO_db = logStoreDataBaseDAO.queryGlobalTransactionDO(867978970L);
    Assertions.assertNotNull(globalTransactionDO_db);
    Assertions.assertEquals(globalTransactionDO_db.getXid(), globalTransactionDO_db.getXid());
    Assertions.assertEquals(globalTransactionDO_db.getBeginTime(), globalTransactionDO_db.getBeginTime());
    Assertions.assertEquals(globalTransactionDO_db.getTransactionName(), globalTransactionDO_db.getTransactionName());
    Assertions.assertEquals(globalTransactionDO_db.getTransactionId(), globalTransactionDO_db.getTransactionId());
    Assertions.assertEquals(globalTransactionDO_db.getStatus(), globalTransactionDO_db.getStatus());
    Assertions.assertEquals(globalTransactionDO_db.getTimeout(), globalTransactionDO_db.getTimeout());
    Assertions.assertEquals(globalTransactionDO_db.getTransactionServiceGroup(), globalTransactionDO_db.getTransactionServiceGroup());
    Assertions.assertEquals(globalTransactionDO_db.getApplicationId(), globalTransactionDO_db.getApplicationId());
    Assertions.assertNotNull(globalTransactionDO_db.getGmtCreate());
    Assertions.assertNotNull(globalTransactionDO_db.getGmtModified());
    String delSql = "delete from global_table where xid= 'abc-123:978786'";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        // delete
        conn.createStatement().execute(delSql);
    } finally {
        IOUtil.close(conn);
    }
}
Also used : GlobalTransactionDO(io.seata.core.store.GlobalTransactionDO) Connection(java.sql.Connection) Test(org.junit.jupiter.api.Test)

Example 2 with GlobalTransactionDO

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

the class LogStoreDataBaseDAOTest method deleteGlobalTransactionDO.

@Test
public void deleteGlobalTransactionDO() throws SQLException {
    GlobalTransactionDO globalTransactionDO = new GlobalTransactionDO();
    globalTransactionDO.setXid("abc-123:555");
    globalTransactionDO.setApplicationData("abc=5454");
    globalTransactionDO.setTransactionServiceGroup("abc");
    globalTransactionDO.setTransactionName("test");
    globalTransactionDO.setTransactionId(12345);
    globalTransactionDO.setTimeout(20);
    globalTransactionDO.setBeginTime(System.currentTimeMillis());
    globalTransactionDO.setApplicationId("test");
    globalTransactionDO.setStatus(1);
    boolean ret = logStoreDataBaseDAO.insertGlobalTransactionDO(globalTransactionDO);
    Assertions.assertTrue(ret);
    // delete
    Assertions.assertTrue(logStoreDataBaseDAO.deleteGlobalTransactionDO(globalTransactionDO));
    // check
    String sql = "select * from global_table where xid= 'abc-123:555'";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sql);
        if (rs.next()) {
            Assertions.assertTrue(false);
        } else {
            Assertions.assertTrue(true);
        }
        rs.close();
    } finally {
        IOUtil.close(conn);
    }
}
Also used : GlobalTransactionDO(io.seata.core.store.GlobalTransactionDO) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.jupiter.api.Test)

Example 3 with GlobalTransactionDO

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

the class LogStoreDataBaseDAOTest method queryGlobalTransactionDO_by_statuses.

@Test
public void queryGlobalTransactionDO_by_statuses() throws SQLException {
    {
        GlobalTransactionDO globalTransactionDO = new GlobalTransactionDO();
        globalTransactionDO.setXid("abc-123:1267");
        globalTransactionDO.setApplicationData("abc=234356");
        globalTransactionDO.setTransactionServiceGroup("abc");
        globalTransactionDO.setTransactionName("test");
        globalTransactionDO.setTransactionId(867978970);
        globalTransactionDO.setTimeout(20);
        globalTransactionDO.setBeginTime(System.currentTimeMillis());
        globalTransactionDO.setApplicationId("test");
        globalTransactionDO.setStatus(1);
        Assertions.assertTrue(logStoreDataBaseDAO.insertGlobalTransactionDO(globalTransactionDO));
    }
    {
        GlobalTransactionDO globalTransactionDO = new GlobalTransactionDO();
        globalTransactionDO.setXid("abc-123:6978");
        globalTransactionDO.setApplicationData("abc=87867978");
        globalTransactionDO.setTransactionServiceGroup("abc");
        globalTransactionDO.setTransactionName("test");
        globalTransactionDO.setTransactionId(143546567);
        globalTransactionDO.setTimeout(20);
        globalTransactionDO.setBeginTime(System.currentTimeMillis());
        globalTransactionDO.setApplicationId("test");
        globalTransactionDO.setStatus(2);
        boolean ret = logStoreDataBaseDAO.insertGlobalTransactionDO(globalTransactionDO);
        Assertions.assertTrue(ret);
    }
    {
        GlobalTransactionDO globalTransactionDO = new GlobalTransactionDO();
        globalTransactionDO.setXid("abc-123:5657");
        globalTransactionDO.setApplicationData("abc=5454");
        globalTransactionDO.setTransactionServiceGroup("abc");
        globalTransactionDO.setTransactionName("test");
        globalTransactionDO.setTransactionId(12345);
        globalTransactionDO.setTimeout(20);
        globalTransactionDO.setBeginTime(System.currentTimeMillis());
        globalTransactionDO.setApplicationId("test");
        globalTransactionDO.setStatus(1);
        boolean ret = logStoreDataBaseDAO.insertGlobalTransactionDO(globalTransactionDO);
        Assertions.assertTrue(ret);
    }
    List<GlobalTransactionDO> globalTransactionDOs = logStoreDataBaseDAO.queryGlobalTransactionDO(new int[] { 1 }, 10);
    Assertions.assertNotNull(globalTransactionDOs);
    Assertions.assertEquals(2, globalTransactionDOs.size());
    if ("abc-123:5657".equals(globalTransactionDOs.get(0).getXid()) && "abc-123:1267".equals(globalTransactionDOs.get(1).getXid())) {
        Assertions.assertTrue(true);
    } else if ("abc-123:5657".equals(globalTransactionDOs.get(1).getXid()) && "abc-123:1267".equals(globalTransactionDOs.get(0).getXid())) {
        Assertions.assertTrue(true);
    } else {
        Assertions.assertTrue(false);
    }
    String delSql = "delete from global_table where xid in ('abc-123:1267', 'abc-123:6978', 'abc-123:5657')";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        conn.createStatement().execute(delSql);
    } finally {
        IOUtil.close(conn);
    }
}
Also used : GlobalTransactionDO(io.seata.core.store.GlobalTransactionDO) Connection(java.sql.Connection) Test(org.junit.jupiter.api.Test)

Example 4 with GlobalTransactionDO

use of io.seata.core.store.GlobalTransactionDO 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 GlobalTransactionDO

use of io.seata.core.store.GlobalTransactionDO 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

GlobalTransactionDO (io.seata.core.store.GlobalTransactionDO)24 Test (org.junit.jupiter.api.Test)10 BranchTransactionDO (io.seata.core.store.BranchTransactionDO)8 Connection (java.sql.Connection)8 GlobalSession (io.seata.server.session.GlobalSession)5 ResultSet (java.sql.ResultSet)4 Jedis (redis.clients.jedis.Jedis)4 ArrayList (java.util.ArrayList)3 DataAccessException (io.seata.common.exception.DataAccessException)2 Date (java.util.Date)2 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1