OracleDataSource连接上的PreparedStatement executeUpdate不会自动提交

时间:2015-04-01 15:16:07

标签: java oracle jdbc java-8 java-7

调用preparedStatement.executeUpdate()成功返回(更新了行数)。但数据库并未反映更新。使用ojdbc7.jar查看此问题(尝试了java 7和java 8 SE)。

final String UPDATE_SQL = "UPDATE myPortfolio SET stock = ? WHERE key = ?";
    final String stock = 'SO';// pre ipo :)
    final long key = 12345l;

    try (Connection conn = pds.getConnection(); PreparedStatement proc = conn.prepareStatement(UPDATE_SQL)) {
        //conn.setAutoCommit(false); --> this works
        conn.setAutoCommit(true); // this is the default...but making sure
        proc.setString(1, stock);
        proc.setLong(2, key);
        int rowcount = proc.executeUpdate();
        //conn.commit(); --> this works

        logger.info("Updated {} rows. SQL = {}. stock = {}, key = {}, InboundKey = {}", rowcount, UPDATE_SQL, stock, key);
        // logs 1 row updated. But DB still shows stale data (old stock) for key 12345l.
    } catch (SQLException E) {
        throw new PersistenceException(E);
    }

    // Pool data source settings
    PoolDataSource        pds = PoolDataSourceFactory.getPoolDataSource();
    pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
    pds.setConnectionPoolName(cacheName);
    pds.setURL(dbUrl);
    pds.setUser(username);
    pds.setPassword(password);

    pds.setMinPoolSize(5);
    pds.setMaxPoolSize(10);
    pds.setInitialPoolSize(7);
    pds.setInactiveConnectionTimeout(10);

1 个答案:

答案 0 :(得分:0)

如果您正在使用executeUpdate()或execute(),那么您需要执行connection.commit()。     如果你不想执行提交,那么你可以去:prepareStatement()不需要commit()。