浏览 3640 次
锁定老帖子 主题:请教下ibatis关于事务的问题
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2008-01-11
我要插入100条记录 情景1: 事务在调用DAO(也就是udao.insertUser(user);)时控制. try { sqlMap.startTransaction(); for (int i = 253; i < 353; i++) { temp = String.valueOf(i); user.setPassword(temp); user.setUsername(temp); udao.insertUser(user); } sqlMap.commitTransaction(); } catch (SQLException e) { System.out.println("事务出错: "); e.printStackTrace(); } finally { try { sqlMap.endTransaction(); } catch (SQLException e) { e.printStackTrace(); } } 从它的日志可看出, 它连接了100次. Connection - {conn-100000} Connection PreparedStatement - {pstm-100001} PreparedStatement: insert into user(username,password) values(?,?) PreparedStatement - {pstm-100001} Parameters: [253, 253] PreparedStatement - {pstm-100001} Types: [java.lang.String, java.lang.String] SimpleDataSource - Returned connection 24408544 to pool. SimpleDataSource - Created connection 12217475. Connection - {conn-100002} Connection PreparedStatement - {pstm-100003} PreparedStatement: insert into user(username,password) values(?,?) PreparedStatement - {pstm-100003} Parameters: [254, 254] PreparedStatement - {pstm-100003} Types: [java.lang.String, java.lang.String] SimpleDataSource - Returned connection 12217475 to pool. SimpleDataSource - SimpleDataSource forcefully closed/removed all connections. SimpleDataSource - SimpleDataSource forcefully closed/removed all connections. 情景2: 事务在在DAO(也就是udao.insertUser(user);)里面控制. 仍然循环插入100条记录. public void insertUser(User user) { SqlMapClient sqlMap = getSqlMap(); try { sqlMap.startTransaction(); sqlMap.insertUser(INSERT_USER, user); sqlMap.commitTransaction(); } catch (SQLException e) { e.printStackTrace(); } finally { try { sqlMap.endTransaction(); } catch (SQLException e) { e.printStackTrace(); } } } 从它的日志可看出, 它也连接了100次. 问题: 据说事务的生命周期在连接的生命周期里面. 某资料上说, 当显式调用sqlMap.startTransaction();的时候, 事务管理不是由ibatis控制. (1)那情景1中,我是100个插入动作看成一个事务, 为什么还要100次连接? (2)ibatis的事务管理能否配置为否, 然后自己在程序里控制? (3)现在看来,情景2的sqlMap.startTransaction()等代码意义不大, 因为这时每一次数据库的操作, 都对应一个连接? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-01-15
补充下: 改用startBatch, 执行5次插入操作, 但从日志看, 仍然是连了5次数据库, 请问有没有N次插入操作, 只连一次数据库?
sqlMap.startBatch(); for (int i = 453; i < 458; i++) { temp = String.valueOf(i); user.setPassword(temp); user.setUsername(temp); udao.insertUser(user); } sqlMap.executeBatch(); 日志: SimpleDataSource - Created connection 24408544. Connection - {conn-100000} Connection PreparedStatement - {pstm-100001} PreparedStatement: insert into user(username,password) values(?,?) PreparedStatement - {pstm-100001} Parameters: [453, 453] PreparedStatement - {pstm-100001} Types: [java.lang.String, java.lang.String] SimpleDataSource - Returned connection 24408544 to pool. SimpleDataSource - Created connection 12217475. Connection - {conn-100002} Connection PreparedStatement - {pstm-100003} PreparedStatement: insert into user(username,password) values(?,?) PreparedStatement - {pstm-100003} Parameters: [454, 454] PreparedStatement - {pstm-100003} Types: [java.lang.String, java.lang.String] SimpleDataSource - Returned connection 12217475 to pool. ...... 多个 Created connection 24408544. |
|
返回顶楼 | |