【发布时间】:2016-07-24 15:22:09
【问题描述】:
以下方法插入两条记录(但此时不提交),然后尝试从前面的语句中读取一条未提交的记录。我用事务包装了代码,并将隔离级别设置为“READ_COMMITTED”,但这似乎不起作用。 read/"SELECT" 语句正在读取未提交的记录。
这怎么可能?我哪里错了?请查看下面的代码并帮助我。真的很感谢~
注意:
我正在使用 BoneCP 来获取数据源。
dbConnectionPool.initConnectionPool(dbName) ,将获取 BoneCPDataSource。
@Override public void testDBCalls() { dBConnectionPool.initConnectionPool("titans"); DataSource dataSource = dBConnectionPool.getDataSource("titans"); DefaultTransactionDefinition definition = new DefaultTransactionDefinition(); definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED); definition.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ); definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); DataSourceTransactionManager txManager = new DataSourceTransactionManager(dataSource); TransactionStatus transactionStatus = txManager.getTransaction(definition); try { try { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); String sql = "INSERT INTO groundwater(external_id,source_type) VALUES (12, 13);"; jdbcTemplate.update(sql); System.out.println("Successfully inserted - 1"); String sql2 = "INSERT INTO groundwater(external_id, source_type,) VALUES(123,45);"; jdbcTemplate.update(sql2); System.out.println("Successfully inserted - 2"); String sql3 = "select gw_id from groundwater where external_id= 123;"; System.out.println("Result : "+jdbcTemplate.queryForInt(sql3)); txManager.commit(transactionStatus); System.out.println("Commiting the trasaction..."); } catch (Exception e) { e.printStackTrace(); txManager.rollback(transactionStatus); System.out.println("Rolling back the transaction"); } } finally { try { dataSource.getConnection().close(); System.out.println("Closing the connection ..."); } catch (SQLException e) { e.printStackTrace(); } } }
【问题讨论】:
-
为什么它不能工作。您确实没有提交任何事情,但一切都是从单个事务运行的,事务始终可以看到它所做的更改。
-
@M.Deinum 谢谢!明白了
标签: spring transactions spring-jdbc spring-transactions