我有一个包含从JDNI加载的PostgreSQL DataSource的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是正确的。
有什么好主意吗?
发布于 2015-09-03 07:44:06
发生此问题是因为我使用Spring,而提交是在Spring创建的事务中完成的。将@Transactional(Transactional.TxType.REQUIRES_NEW)添加到我的方法中可以解决问题。这个文章描述了使用@Transactional时常见的缺陷,这帮助我找到了解决问题的方法。
https://stackoverflow.com/questions/32113132
复制相似问题