xiaoxiangyeyu0 commented on issue #7686: URL: https://github.com/apache/incubator-seata/issues/7686#issuecomment-3388818942
> > > 你们好!我已经试了2.4版本,似乎还是有些问题: > > > 验证时没有使用[@transactional](https://github.com/transactional)注解: > > > 首先,日志中显示似乎有join事务的处理了: 2025-10-10 14:24:37.098 | �[34m INFO 8308�[0;39m | �[1;33mhttp-nio-48081-exec-10 [TID: N/A]�[0;39m �[1;32mo.a.seata.tm.api.TransactionalTemplate �[0;39m | join into a existing global transaction,xid=192.168.100.81:8091:3261279418629289739 > > > 但是我发现pg的pending事务表pg_prepared_xacts,仍然有很多记录,然后,我就发现了一个问题: seata现在对于每一个select语句(即使没有for update),也会创建一个分支事务 > > > 也就是说,现在的问题是: xa模式下,没有transactional注解时,怎么才能让select语句不产生分支事务呢? > > > > > > 我用sample试了是正常的,你的[@ds](https://github.com/ds)是怎么切换,一个线程doSth1一个数据源,然后调用doSth2一个数据源吗?如果是这样,你可以先不切换数据源试试 > > 哦,我没说清楚,我再从新描述下问题: 现在问题不是[@ds](https://github.com/ds)没有正常切换数据源, 因为如果不加[@transactional](https://github.com/transactional)注解,数据源是可以切换的, 现在的问题是:如果不加[@transactional](https://github.com/transactional)注解后,seata对于service中的所有select语句,都会生成一个新的分支事务,这样会使得一个全局事务会产生大量的分支事务,我加一下我这里的日志,可以看到每个select都会产生branch register success的日志,pg里也能看到创建了分支事务,这样对数据库的压力应该会太大,能否做到select语句不产生分支事务?或者加入已有的分支事务? > > `[preHandle][开始请求 URL(/rpc-api/system/teacher/list-by-ids) 参数([274])] a.seata.tm.api.TransactionalTemplate �[0;39m | join into a existing global transaction,xid=192.168.100.81:8091:3261279418629289739 c.m.t.d.m.t.T.selectBatchIds �[0;39m | ==> Preparing: SELECT id, remark, login_id, passwd, invalid_flag, lang, c.m.t.d.m.t.T.selectBatchIds �[0;39m | ==> Parameters: 274(Long) a.seata.rm.AbstractResourceManager �[0;39m | branch register success, xid:192.168.100.81:8091:3261279418629289739, branchId:3261279418629289743, lockKeys:null c.m.t.d.m.t.T.selectBatchIds �[0;39m | <== Total: 1 c.m.s.d.m.u.A.selectById �[0;39m | ==> Preparing: SELECT id, username, password, nickname, remark, dept_id, post_ids, email, mobile c.m.s.d.m.u.A.selectById �[0;39m | ==> Parameters: 274(Long) a.seata.rm.AbstractResourceManager �[0;39m | branch register success, xid:192.168.100.81:8091:3261279418629289739, branchId:3261279418629289744, lockKeys:null c.m.s.d.m.u.A.selectById �[0;39m | <== Total: 1 c.m.s.d.m.u.A.updateById �[0;39m | ==> Preparing: UPDATE system_users SET remark = ?, update_time = ?, updater = ? WHERE id = ? AND deleted = false c.m.s.d.m.u.A.updateById �[0;39m | ==> Parameters: test(String), 2025-10-10T15:24:37.150128600(LocalDateTime), null, 274(Long) a.seata.rm.AbstractResourceManager �[0;39m | branch register success, xid:192.168.100.81:8091:3261279418629289739, branchId:3261279418629289745, lockKeys:null c.m.s.d.m.u.A.updateById �[0;39m | <== Updates: 1 c.m.s.d.m.u.A.selectById �[0;39m | ==> Preparing: SELECT id, username, password, nickname, remark, dept_id, post_ids, email, mobile, sex, avatar, status, c.m.s.d.m.u.A.selectById �[0;39m | ==> Parameters: 274(Long) a.seata.rm.AbstractResourceManager �[0;39m | branch register success, xid:192.168.100.81:8091:3261279418629289739, branchId:3261279418629289746, lockKeys:null c.m.s.d.m.u.A.selectById �[0;39m | <== Total: 1 c.m.s.d.m.p.UserRoleMapper.selectList �[0;39m | ==> Preparing: SELECT id, user_id, role_id, create_time, update_time, creator, updater, deleted FROM system_user_role WHERE deleted = false AND (user_id = ?) c.m.s.d.m.p.UserRoleMapper.selectList �[0;39m | ==> Parameters: 274(Long) a.seata.rm.AbstractResourceManager �[0;39m | branch register success, xid:192.168.100.81:8091:3261279418629289739, branchId:3261279418629289747, lockKeys:null c.m.s.d.m.p.UserRoleMapper.selectList �[0;39m | <== Total: 0 c.m.s.d.m.u.A.selectById �[0;39m | ==> Preparing: SELECT id, username, password, nickname, remark, dept_id, post_ids, email, mobile, sex, avatar, status, c.m.s.d.m.u.A.selectById �[0;39m | ==> Parameters: 274(Long) a.seata.rm.AbstractResourceManager �[0;39m | branch register success, xid:192.168.100.81:8091:3261279418629289739, branchId:3261279418629289748, lockKeys:null` 你这里是因为没有@transactional后,每次执行完sql后会关闭连接,执行下一个sql的时候,会重新拿一个新的connection连接,每个连接都会注册一个分支事务。 这种情况是本地切换数据源的事务,我记得ds是有本地分布式事务功能的,也是[funky-eyes](https://github.com/funky-eyes)大佬实现的,如果要用seata,我建议doSth1和doSth2方法把@transactional和@GlobalTransactional都加上,doSth1调用doSth2使用远程调用。 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
