mysql同⼀个事务中先插⼊再查询与先删除再查询结果分析
⼀,现象展⽰
1、先插⼊再查询
BEGIN;
INSERT INTO video (CREATE_TIME, UPDATE_TIME, VERSION, VID, VIDEO_NAME, SPEED, RATE, HORIZONTAL, VERTICAL, SIZE, LIB_ID, ALARM_THRESH VALUES(NOW(),NOW(),0,'vidtest','test.mp4',1,1,1,1,'1',1,0.1,20,20,'');
SELECT * FROM video WHERE VID = 'vidtest';
ROLLBACK;
结果:
在事务内部的SELECT语句可以查询到数据,但是回滚后,不能查询到数据。
2、先删除再查询
BEGIN;
DELETE FROM video WHERE VID = 'vidtest';
mysql删除重复的数据保留一条SELECT * FROM video WHERE VID = 'vidtest';
ROLLBACK;
结果:
在事务内部的查询不能查询到数据,但是回滚后,可以查询到数据。
⼆、原因说明:
在同⼀个事务中,数据需要保证⼀致性,也就是说,在当前的事务中,删除了就是看不到了,插⼊了就是多了⼀条数据,但这些操作都是在
当前事务内部的。对于外界是隔离的。如果我们开启事务插⼊⼀条数据,还没有提交,这时新开⼀个事务,去查询数据,是发现不了未提交
数据的。
三,基于mybatis进⾏测试
1、先插⼊再更新
//这两个操作是在同⼀个⽅法中⽅法是通过Transactional注解实现事务的
@Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
//插⼊⼀条数据
int i = videoDao.insert(videoPo);
//更新⼀条数据
int j = videoDao.Id(),vid);
控制台打印的sql语句
Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5cfedd40]
JDBC Connection [ProxyConnection[sql.jdbc.JDBC4Connection@6624f1eb]]] will be managed by Spring
==> Preparing: INSERT INTO video (CREATE_TIME, UPDATE_TIME, VERSION, VID, VIDEO_NAME, SPEED, RATE, HORIZONTAL, VERTICAL, SIZE, LIB_ID, A ==> Parameters: vid60c7d86a-111b-45ec-b25e-2100b6f9a588(String), 1.mp4(String), 1(Integer), 1(Integer), 50(Integer), 20(Integer), 30*30(String), 1(Long), 0.1 <== Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5cfedd40]
Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5cfedd40] from current transaction
==> Preparing: UPDATE video SET VID = ? WHERE ID = ?
==> Parameters: vid14(String), 14(Long)
<== Updates: 1
mybatis会将两条执⾏语句在同⼀个sqlSession中执⾏。
2、先删除再查询
//这两个操作是在同⼀个⽅法中⽅法是通过Transactional注解实现事务的
@Transactional(rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
//删除⼀条数据
int i = videoDao.deleteVedioByVid(vid);
//查询被删除的数据
VideoPo videoPo = VideoByVid(vid);
控制台打印的sql语句
Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3b1ca134]
JDBC Connection [ProxyConnection[sql.jdbc.JDBC4Connection@6624f1eb]]] will be managed by Spring
==> Preparing: DELETE FROM video WHERE VID = ?
==> Parameters: vidtest(String)
<== Updates: 1
Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3b1ca134]
Creating a new SqlSession
Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@55d43e72]
JDBC Connection [ProxyConnection[sql.jdbc.JDBC4Connection@1dfa23fe]]] will be managed by Spring
==> Preparing: SELECT * FROM video WHERE VID = ?
==> Parameters: vidtest(String)
<== Columns: ID, CREATE_TIME, UPDATE_TIME, VERSION, VID, VIDEO_NAME, SPEED, RATE, HORIZONTAL, VERTICAL, SIZE, LIB_ID, ALARM_THRESHO <== Row: 12, 2018-10-19 15:05:15.0, 2018-10-19 15:05:15.0, 0, vidtest, test.mp4, 1, 1, 1, 1, 1, 1, 0.1000, 20, 20,
<== Total: 1
mybatis创建了两个sqlSession。
后来我⼜测试了先删除再更新的场景,发现更新失败。
结论:mybatis会再插⼊或删除后,进⾏更新操作时使⽤同⼀个sqlSession,查询时新建sqlSession。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论