【问题标题】:java.sql.SQLException: Generated keys not requested. in JBossjava.sql.SQLException:未请求生成的密钥。在 JBoss 中
【发布时间】:2015-09-14 07:33:30
【问题描述】:

我有在 Wildfly 9.0.1.Final 上运行的基于 struts 的应用程序,数据库是 MySQL。 之前的代码使用运行没有任何错误。当时JBoss版本是4.0.4.GA。

现在我已经在 Wildfly 9.0.1.Final 上部署了代码,当我添加一些新行时出现错误。

堆栈跟踪:

Caused by: java.sql.SQLException: Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or Connection.prepareStatement().
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
    at com.mysql.jdbc.StatementImpl.getGeneratedKeys(StatementImpl.java:1803)
    at org.jboss.jca.adapters.jdbc.WrappedStatement.getGeneratedKeys(WrappedStatement.java:1105)
    at com.aldorsolutions.webfdms.database.DatabasePeer.insert(DatabasePeer.java:90)
    ... 48 more

代码:

    try {
                pstmt = getInsertSql((DatabaseTransaction)t,p);
                pstmt.executeUpdate(); // Execute the SQL

                ResultSet rs = pstmt.getGeneratedKeys();
                // ResultSet rs = stmt.executeQuery("SELECT @@IDENTITY");

                if (rs.next()) {
                    autoinc = rs.getInt(1);
                    p.setId(autoinc);
                }

            } catch( SQLException e ) {
                // logger.error("SQLException in DatabasePeer.save: ", e);
                throw new PersistenceException("PersistentObj:" + p.getClass().getName() + " Message: " + e.getMessage(), e);
            }

getInsertSql() 的代码:

try {
                DbSpeedData item=(DbSpeedData)p;
                connection = t.getConnection();
                pstmt = connection.prepareStatement(
                    "INSERT INTO speeddata (Identity, TabCategory, Locale, " +
                    "LocationId, TabData, SortSequence) VALUES (0,?,?,?,?,?)");
                pstmt.setString(1, item.getCategory());
                pstmt.setInt   (2, item.getLocale());
                pstmt.setInt   (3, item.getLocationId());
                pstmt.setString(4, item.getData());
                pstmt.setInt   (5, item.getSortSequence());
                return pstmt;
            }
            catch (java.sql.SQLException e){
                throw new com.aldorsolutions.webfdms.database.PersistenceException("DbSpeedDataPeer.Insert",e);
            }

我google了一下,发现需要添加 Statement.RETURN_GENERATED_KEYSpstmt.executeUpdate();

但是之前使用的代码在 JBoss 4.0.4.GA 上可以正常运行,而我根本没有更改代码。唯一的变化是 JBoss 升级为 Wildfly。

可能是什么问题? 请帮忙。

【问题讨论】:

  • @JamesR.Perkins 你能给我任何意见吗?
  • @TerrenceCurran 你可以吗?
  • @BalusC 你可以为此贡献一些东西吗?

标签: java mysql jboss wildfly mysql-connector


【解决方案1】:

你能附上getInsertSql的代码吗?

您需要在创建PreparedStatement 时传递Statement.RETURN_GENERATED_KEYS 选项。例如:

pstmt = connection.prepareStatement(
    "INSERT INTO speeddata (Identity, TabCategory, Locale, " +
    "LocationId, TabData, SortSequence) VALUES (0,?,?,?,?,?)",
    Statement.RETURN_GENERATED_KEYS
);

【讨论】:

  • 是的。我补充说
  • 但我想知道我没有对源代码进行任何修改,只是更改了 JBoss 版本。
  • 更新了我的答案...如果这是导致此问题的 jboss 版本的更改,我会感到非常惊讶。您确定您还没有更改数据库驱动程序吗?如果您可以在不指定 Statement.RETURN_GENERATED_KEYS 的情况下调用 getGeneratedKeys(),我会感到非常惊讶
  • 是的,这是不使用Statement.RETURN_GENERATED_KEYS的工作代码
  • 一件事是我在我的代码中使用了 mysql-connector 5.0.5 版,而在 jboss 上我已经部署了 5.1.36 版。这会导致这个问题吗?
猜你喜欢
  • 2011-11-02
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 2016-11-28
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多