【问题标题】:JDBC Auto-Commit Not Working with PostgreSQL 9 DriverJDBC 自动提交不适用于 PostgreSQL 9 驱动程序
【发布时间】:2015-09-03 15:44:06
【问题描述】:

我有一个带有从 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 是正确的。

请问有什么问题吗?

【问题讨论】:

  • 请贴出相关代码
  • 也许是一些春天的魔法。你在使用@Transactional 注释吗?运行 DML 语句的完整代码是什么?
  • 不幸的是,添加 @Transactional 并没有帮助。 DML 语句的编码如下:public void insertItem(Item item) { getJdbcTemplate().update(item.getSQLInsert()); }

标签: java spring postgresql jdbc


【解决方案1】:

之所以会出现这个问题,是因为我使用的是 Spring Batch,而我的提交是在 Spring Batch 创建的事务中完成的。在我的方法中添加@Transactional(Transactional.TxType.REQUIRES_NEW) 可以解决问题。 这个article 描述了使用@Transactional 时的常见陷阱,它帮助我找到了解决问题的方法。

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 2016-12-23
    • 1970-01-01
    • 2013-10-25
    • 2021-08-30
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多