【发布时间】: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_KEYS 到 pstmt.executeUpdate();。
但是之前使用的代码在 JBoss 4.0.4.GA 上可以正常运行,而我根本没有更改代码。唯一的变化是 JBoss 升级为 Wildfly。
可能是什么问题? 请帮忙。
【问题讨论】:
-
@JamesR.Perkins 你能给我任何意见吗?
-
@TerrenceCurran 你可以吗?
-
@BalusC 你可以为此贡献一些东西吗?
标签: java mysql jboss wildfly mysql-connector