我有以下存储库:
public interface PlayerRealBalanceRepository extends JpaRepository<PlayerRealBalance, Long> {
@Modifying
@Query("update PlayerRealBalance balance set balance.balance = (balance.balance + ?1) where balance.id = ?2")
public void increaseBalance(long amount, long balanceId);
}
我的问题 - 这个查询线程安全吗?如果这样的2个并发查询完全同时执行怎么办?我是否必须使用锁定方法才能正常工作?
谢谢!
答案 0 :(得分:0)
它将遇到ACID(原子性,一致性,隔离性,耐久性)问题。因此,更好地使用事务请参考spring transaction management here这里提供的是日志(自解释)如果实现事务管理会发生什么(虽然我使用Mybatis)
2014-02-11 16:48:21,008 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Switching JDBC Connection [jdbc:oracle:thin] to manual commit
2014-02-11 16:48:21,013 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession
2014-02-11 16:48:21,021 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@16a23cf]
2014-02-11 16:48:21,091 [main] DEBUG o.m.s.t.SpringManagedTransaction - JDBC Connection [jdbc:oracle:thin:] will be managed by Spring
// does the db operation
2014-02-11 16:48:21,792 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@16a23cf]
2014-02-11 16:48:21,792 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Initiating transaction commit
2014-02-11 16:48:21,792 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Committing JDBC transaction on Connection [jdbc:oracle:thin:]
2014-02-11 16:48:21,792 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization committing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@16a23cf]
2014-02-11 16:48:21,792 [main] DEBUG org.mybatis.spring.SqlSessionUtils - Transaction synchronization closing SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@16a23cf]
2014-02-11 16:48:21,792 [main] DEBUG o.s.jdbc.datasource.DataSourceUtils - Resetting isolation level of JDBC Connection [jdbc:oracle:thin:] to 2
2014-02-11 16:48:21,792 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Releasing JDBC Connection [jdbc:oracle:thin:@] after transaction
2014-02-11 16:48:21,792 [main] DEBUG o.s.jdbc.datasource.DataSourceUtils - Returning JDBC Connection to DataSource