use of io.seata.server.session.BranchSession 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();
}
}
}
use of io.seata.server.session.BranchSession 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();
}
}
}
use of io.seata.server.session.BranchSession in project seata by seata.
the class SessionConverterTest method testConvertBranchTransactionDONotNull.
@Test
public void testConvertBranchTransactionDONotNull() {
BranchSession branchSession = new BranchSession();
branchSession.setXid("192.168.158.80:8091:39372760251957248");
branchSession.setResourceId("jdbc:mysql://116.62.62.62/seata-storage");
branchSession.setTransactionId(39372760251957248L);
branchSession.setBranchId(39372760251957111L);
branchSession.setBranchType(BranchType.XA);
branchSession.setResourceGroupId("abc");
branchSession.setClientId("storage-server:192.168.158.80:11934");
branchSession.setStatus(BranchStatus.PhaseOne_Failed);
branchSession.setApplicationData("abc=123");
BranchTransactionDO branchTransactionDO = SessionConverter.convertBranchTransactionDO(branchSession);
Assertions.assertEquals(branchSession.getXid(), branchTransactionDO.getXid());
Assertions.assertEquals(branchSession.getResourceId(), branchTransactionDO.getResourceId());
Assertions.assertEquals(branchSession.getTransactionId(), branchTransactionDO.getTransactionId());
Assertions.assertEquals(branchSession.getBranchId(), branchTransactionDO.getBranchId());
Assertions.assertEquals(branchSession.getBranchType().name(), branchTransactionDO.getBranchType());
Assertions.assertEquals(branchSession.getResourceGroupId(), branchTransactionDO.getResourceGroupId());
Assertions.assertEquals(branchSession.getClientId(), branchTransactionDO.getClientId());
Assertions.assertEquals(branchSession.getStatus().getCode(), branchTransactionDO.getStatus());
Assertions.assertEquals(branchSession.getApplicationData(), branchTransactionDO.getApplicationData());
}
use of io.seata.server.session.BranchSession in project seata by seata.
the class WriteStoreMultithreadTest method main.
public static void main(String[] args) throws Exception {
TransactionStoreManager transactionStoreManager = new FileTransactionStoreManager("data", new SessionManager() {
@Override
public void destroy() {
}
@Override
public void addGlobalSession(GlobalSession session) throws TransactionException {
}
@Override
public GlobalSession findGlobalSession(String xid) {
return null;
}
@Override
public GlobalSession findGlobalSession(String xid, boolean withBranchSessions) {
return null;
}
@Override
public void updateGlobalSessionStatus(GlobalSession session, GlobalStatus status) throws TransactionException {
}
@Override
public void removeGlobalSession(GlobalSession session) throws TransactionException {
}
@Override
public void addBranchSession(GlobalSession globalSession, BranchSession session) throws TransactionException {
}
@Override
public void updateBranchSessionStatus(BranchSession session, BranchStatus status) throws TransactionException {
}
@Override
public void removeBranchSession(GlobalSession globalSession, BranchSession session) throws TransactionException {
}
@Override
public Collection<GlobalSession> allSessions() {
return null;
}
@Override
public List<GlobalSession> findGlobalSessions(SessionCondition condition) {
List<GlobalSession> globalSessions = new ArrayList<>();
int begin = 10000;
int num = 1000;
for (int i = begin; i < begin + num; i++) {
BranchSession branchSession1 = new BranchSession();
branchSession1.setTransactionId(i);
branchSession1.setBranchId(begin + num + (i - begin) * 2);
branchSession1.setResourceId("mockDbkeY1");
BranchSession branchSession2 = new BranchSession();
branchSession2.setTransactionId(i);
branchSession2.setBranchId(begin + num + (i - begin) * 2 + 1);
branchSession2.setResourceId("mockDbkeY2");
GlobalSession globalSession = new GlobalSession(appname, vgroup, instname, 60000);
try {
globalSession.add(branchSession1);
globalSession.add(branchSession2);
globalSessions.add(globalSession);
} catch (Exception exx) {
}
}
return globalSessions;
}
@Override
public <T> T lockAndExecute(GlobalSession globalSession, GlobalSession.LockCallable<T> lockCallable) throws TransactionException {
return null;
}
@Override
public void onBegin(GlobalSession globalSession) throws TransactionException {
}
@Override
public void onStatusChange(GlobalSession globalSession, GlobalStatus status) throws TransactionException {
}
@Override
public void onBranchStatusChange(GlobalSession globalSession, BranchSession branchSession, BranchStatus status) throws TransactionException {
}
@Override
public void onAddBranch(GlobalSession globalSession, BranchSession branchSession) throws TransactionException {
}
@Override
public void onRemoveBranch(GlobalSession globalSession, BranchSession branchSession) throws TransactionException {
}
@Override
public void onClose(GlobalSession globalSession) throws TransactionException {
}
@Override
public void onEnd(GlobalSession globalSession) throws TransactionException {
}
});
long beginWriteMills = System.currentTimeMillis();
for (int i = 0; i < threadNum; i++) {
final int threadNo = i;
Thread thread = new Thread(() -> {
write(transactionStoreManager, threadNo);
});
thread.start();
}
countDownLatch.await();
long endWriteMills = System.currentTimeMillis();
System.out.println("thread nums:" + threadNum + ", per_thread_trx_num:" + per_thread_trx_num + " ,cost" + (endWriteMills - beginWriteMills));
}
use of io.seata.server.session.BranchSession in project seata by seata.
the class DefaultCoreTest method doGlobalCommitCommitTest.
/**
* Do global commit test.
*
* @param xid the xid
* @throws Exception the exception
*/
@ParameterizedTest
@MethodSource("xidProvider")
public void doGlobalCommitCommitTest(String xid) throws Exception {
globalSession = SessionHolder.findGlobalSession(xid);
BranchSession branchSession = SessionHelper.newBranchByGlobal(globalSession, BranchType.XA, resourceId, applicationData, "t1:1", clientId);
globalSession.addBranch(branchSession);
globalSession.changeBranchStatus(branchSession, BranchStatus.PhaseOne_Done);
core.mockCore(BranchType.XA, new MockCore(BranchStatus.PhaseTwo_Committed, BranchStatus.PhaseOne_Done));
core.doGlobalCommit(globalSession, false);
Assertions.assertEquals(globalSession.getStatus(), GlobalStatus.Committed);
}
Aggregations