我有一个带有从 JDNI 加载的 PostgreSQL 数据源的 spring 应用程序:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/DB" expected-type="javax.sql.DataSource" />
我用 JDBCTemplate 连接了它
// DataSource is Autowired
jdbcTemplate = new JdbcTemplate(dataSource);
并且自动提交设置为 true
<Resource name="jdbc/DB" auth="Container" type="javax.sql.DataSource"
username="postgres" password="localPostgres"
url="jdbc:postgresql://localhost:5432/postgres"
driverClassName="org.postgresql.Driver"
defaultAutoCommit="true"/>
当我尝试使用简单的 SQL 插入语句插入项目时,更改未提交到数据库。
jdbcTemplate.update("INSERT INTO items (name, surname) VALUES ('samantha', 'catania')";
直接在数据库控制台中输入 SQL 语句,项目被插入,即 SQL 是正确的。
请问有什么问题吗?
最佳答案
发生此问题是因为我正在使用 Spring Batch 并且我的提交是在 Spring Batch 创建的事务中完成的。将 @Transactional(Transactional.TxType.REQUIRES_NEW)
添加到我的方法可以解决问题。
这article描述了使用 @Transactional
时的常见陷阱,这帮助我找到了问题的解决方案。
关于java - JDBC 自动提交不适用于 PostgreSQL 9 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32113132/