Search in sources :

Example 21 with GlobalSession

use of io.seata.server.session.GlobalSession in project seata by seata.

the class DataBaseSessionManagerTest method test_removeBranchSession.

@Test
public void test_removeBranchSession() throws Exception {
    GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(globalSession.getTransactionId());
    globalSession.setXid(xid);
    globalSession.setTransactionId(146757978);
    globalSession.setBeginTime(System.currentTimeMillis());
    globalSession.setApplicationData("abc=878s");
    globalSession.setStatus(GlobalStatus.Begin);
    BranchSession branchSession = new BranchSession();
    branchSession.setBranchId(UUIDGenerator.generateUUID());
    branchSession.setXid(xid);
    branchSession.setTransactionId(globalSession.getTransactionId());
    branchSession.setBranchId(1L);
    branchSession.setResourceGroupId("my_test_tx_group");
    branchSession.setResourceId("tb_1");
    branchSession.setLockKey("t_1");
    branchSession.setBranchType(BranchType.AT);
    branchSession.setApplicationData("{\"data\":\"test\"}");
    branchSession.setStatus(BranchStatus.PhaseOne_Done);
    sessionManager.addBranchSession(globalSession, branchSession);
    sessionManager.removeBranchSession(globalSession, branchSession);
    String sql = "select * from branch_table where xid= '" + xid + "'";
    String delSql = "delete from branch_table where xid= '" + xid + "'" + ";" + "delete from global_table where xid= '" + xid + "'";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sql);
        if (rs.next()) {
            Assertions.assertTrue(false);
        } else {
            Assertions.assertTrue(true);
        }
        conn.createStatement().execute(delSql);
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.jupiter.api.Test)

Example 22 with GlobalSession

use of io.seata.server.session.GlobalSession in project seata by seata.

the class DataBaseSessionManagerTest method test_addGlobalSession.

@Test
public void test_addGlobalSession() throws TransactionException, SQLException {
    GlobalSession session = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(session.getTransactionId());
    session.setXid(xid);
    session.setTransactionId(146757978);
    session.setBeginTime(System.currentTimeMillis());
    session.setApplicationData("abc=878s");
    session.setStatus(GlobalStatus.Begin);
    sessionManager.addGlobalSession(session);
    String sql = "select * from global_table where xid= '" + xid + "'";
    String delSql = "delete from global_table where xid= '" + xid + "'";
    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 {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.jupiter.api.Test)

Example 23 with GlobalSession

use of io.seata.server.session.GlobalSession in project seata by seata.

the class DataBaseSessionManagerTest method test_updateBranchSessionStatus.

@Test
public void test_updateBranchSessionStatus() throws Exception {
    GlobalSession globalSession = GlobalSession.createGlobalSession("test", "test", "test123", 100);
    String xid = XID.generateXID(globalSession.getTransactionId());
    globalSession.setXid(xid);
    globalSession.setTransactionId(146757978);
    globalSession.setBeginTime(System.currentTimeMillis());
    globalSession.setApplicationData("abc=878s");
    globalSession.setStatus(GlobalStatus.Begin);
    BranchSession branchSession = new BranchSession();
    branchSession.setBranchId(UUIDGenerator.generateUUID());
    branchSession.setXid(xid);
    branchSession.setTransactionId(globalSession.getTransactionId());
    branchSession.setBranchId(1L);
    branchSession.setResourceGroupId("my_test_tx_group");
    branchSession.setResourceId("tb_1");
    branchSession.setLockKey("t_1");
    branchSession.setBranchType(BranchType.AT);
    branchSession.setApplicationData("{\"data\":\"test\"}");
    branchSession.setStatus(BranchStatus.PhaseOne_Done);
    sessionManager.addBranchSession(globalSession, branchSession);
    branchSession.setStatus(BranchStatus.PhaseOne_Timeout);
    sessionManager.updateBranchSessionStatus(branchSession, BranchStatus.PhaseOne_Timeout);
    String sql = "select * from branch_table where xid= '" + xid + "'";
    String delSql = "delete from branch_table where xid= '" + xid + "'" + ";" + "delete from global_table where xid= '" + xid + "'";
    Connection conn = null;
    try {
        conn = dataSource.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sql);
        if (rs.next()) {
            Assertions.assertTrue(true);
            Assertions.assertEquals(rs.getInt("status"), BranchStatus.PhaseOne_Timeout.getCode());
        } else {
            Assertions.assertTrue(false);
        }
        conn.createStatement().execute(delSql);
    } finally {
        if (conn != null) {
            conn.close();
        }
    }
}
Also used : GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Test(org.junit.jupiter.api.Test)

Example 24 with GlobalSession

use of io.seata.server.session.GlobalSession in project seata by seata.

the class SessionConverterTest method testConvertGlobalSessionNull.

@Test
public void testConvertGlobalSessionNull() {
    GlobalTransactionDO globalTransactionDO = null;
    GlobalSession globalSession = SessionConverter.convertGlobalSession(globalTransactionDO);
    Assertions.assertNull(globalSession);
}
Also used : GlobalTransactionDO(io.seata.core.store.GlobalTransactionDO) GlobalSession(io.seata.server.session.GlobalSession) Test(org.junit.jupiter.api.Test)

Example 25 with GlobalSession

use of io.seata.server.session.GlobalSession in project seata by seata.

the class SessionConverterTest method testConvertGlobalSessionNotNull.

@Test
public void testConvertGlobalSessionNotNull() {
    GlobalTransactionDO globalTransactionDO = new GlobalTransactionDO();
    Date date = new Date();
    long now = date.getTime();
    globalTransactionDO.setXid("192.168.158.80:8091:39372760251957248");
    globalTransactionDO.setTransactionId(39372760251957248L);
    globalTransactionDO.setStatus(1);
    globalTransactionDO.setApplicationId("credit");
    globalTransactionDO.setTransactionServiceGroup("fsp_tx");
    globalTransactionDO.setTransactionName("createOrder");
    globalTransactionDO.setTimeout(60);
    globalTransactionDO.setBeginTime(now);
    globalTransactionDO.setApplicationData("id=1,product=2");
    globalTransactionDO.setGmtCreate(date);
    globalTransactionDO.setGmtModified(date);
    GlobalSession globalSession = SessionConverter.convertGlobalSession(globalTransactionDO);
    Assertions.assertEquals(globalTransactionDO.getXid(), globalSession.getXid());
    Assertions.assertEquals(globalTransactionDO.getTransactionId(), globalSession.getTransactionId());
    Assertions.assertEquals(globalTransactionDO.getStatus(), globalSession.getStatus().getCode());
    Assertions.assertEquals(globalTransactionDO.getApplicationId(), globalSession.getApplicationId());
    Assertions.assertEquals(globalTransactionDO.getTransactionServiceGroup(), globalSession.getTransactionServiceGroup());
    Assertions.assertEquals(globalTransactionDO.getTransactionName(), globalSession.getTransactionName());
    Assertions.assertEquals(globalTransactionDO.getTimeout(), globalSession.getTimeout());
    Assertions.assertEquals(globalTransactionDO.getBeginTime(), globalSession.getBeginTime());
    Assertions.assertEquals(globalTransactionDO.getApplicationData(), globalSession.getApplicationData());
}
Also used : GlobalTransactionDO(io.seata.core.store.GlobalTransactionDO) GlobalSession(io.seata.server.session.GlobalSession) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

GlobalSession (io.seata.server.session.GlobalSession)73 BranchSession (io.seata.server.session.BranchSession)33 Test (org.junit.jupiter.api.Test)32 Connection (java.sql.Connection)10 TransactionException (io.seata.core.exception.TransactionException)9 ArrayList (java.util.ArrayList)8 SessionCondition (io.seata.server.session.SessionCondition)7 GlobalTransactionEvent (io.seata.core.event.GlobalTransactionEvent)6 BranchStatus (io.seata.core.model.BranchStatus)6 ResultSet (java.sql.ResultSet)6 GlobalTransactionDO (io.seata.core.store.GlobalTransactionDO)5 LockManager (io.seata.server.lock.LockManager)5 FileLockManagerForTest (io.seata.server.lock.file.FileLockManagerForTest)5 FileTransactionStoreManager (io.seata.server.storage.file.store.FileTransactionStoreManager)5 List (java.util.List)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 BranchTransactionException (io.seata.core.exception.BranchTransactionException)4 TransactionWriteStore (io.seata.server.storage.file.TransactionWriteStore)4 GlobalStatus (io.seata.core.model.GlobalStatus)3 SessionManager (io.seata.server.session.SessionManager)3