Spring的事务管理难点剖析(5):联合军种作战的混乱(二)

2014-11-24 08:46:58 · 作者: · 浏览: 5
ds BaseService{

10

11 @Autowired

12 private JdbcTemplate jdbcTemplate;

13

14 public void addScore(String userName, int toAdd) {

15 String sql = "UPDATE t_user u SET u.score = u.score + WHERE user_name = ";

16 jdbcTemplate.update(sql, toAdd, userName);

17 BasicDataSource basicDataSource = (BasicDataSource) jdbcTemplate.getDataSource();

18 //①查看此处数据库激活的连接数量

19 System.out.println("[scoreUserService.addScore]激活连接数量:"

20 +basicDataSource.getNumActive());

21 }

22 }


Spring关键的配置文件代码如下所示:

view sourceprint 01

02

03

04 class="org.springframework.orm.hibernate3.HibernateTransactionManager"

05 p:sessionFactory-ref="sessionFactory"/>

06

07

08

09

10 expression="within(com.baobaotao.mixdao.BaseService+)"/>

11

12 advice-ref="hiberAdvice"/>

13

14

15

16

17

18

19

20

启动Spring容器,执行UserService#logon()方法,可以查看到如下的执行日志:

引用
before userService.logon()..

①在执行userService.logon()后,Spring开启一个事务
Creating new transaction with name [com.baobaotao.mixdao.UserService.logon]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
opened session at timestamp: 13009379637
Opened new Session [org.hibernate.impl.SessionImpl@c5f468] for Hibernate transaction

Exposing Hibernate transaction as JDBC transaction [jdbc:mysql://localhost:3306/sampledb, UserName=root@localhost , MySQL-AB JDBC Driver]
before userService.updateLastLogonTime()..

②userService.updateLastLogonTime()执行时自动绑定到①处开启的Session中
Found thread-bound Session for HibernateTemplate
loading entity: [com.baobaotao.User#tom]
about to open PreparedStatement (open PreparedStatements: 0, globally: 0)

about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
Not closing pre-bound Hibernate Session after HibernateTemplate
end updateLastLogonTime()..

before scoreService.addScore()..

③scoreService.addScore()执行时绑定到①处开启的Session中,并加入其所对应的事务中
Found thread-bound Session [org.hibernate.impl.SessionImpl@c5f468] for Hibernate
transaction
Participating in existing transaction

SQL update affected 1 rows

④此时数据源只打开了一个连接
[scoreUserService.addScore]激活连接数量:1
end scoreService.addScore()..
Initiating transaction commit

⑤提交Hibernate的事务,它将触发一级缓存到数据库的同步
Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@c5f468]
commit
processing flush-time cascades
dirty checking collections
Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
listing entities:
com.baobaotao.User{lastLogonTime=1300937963882, score=10, userName=tom, password=123456}
re-enabling autocommit

⑥提效Session底层所绑定的JDBC Connection所对应的事务
committed JDBC Connection
transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
Closing Hibernate Session [org.hibernate.impl.SessionImpl@c5f468] after transaction
Closing Hibernate Session
releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
transaction completed on session with on_close connection release mode; be sure to close t