1 Reply Latest reply on Sep 15, 2015 3:55 AM by mahendra kawde

    java.sql.SQLException: Generated keys not requested.

    mahendra kawde Newbie

      I am having struts based application that runs on Wildfly.Final and database is MySQL.

      Earlier the code use to run without any error. At that time JBoss version was 4.0.4.GA.

       

       

      Now I have deployed code on Wildfly and I am getting error when I add some new row.

       

       

      Stacktrace:

       

       

          ERROR [com.aldorsolutions.webfdms.beans.FdmsDb] (default task-6) getSpeedDataOptionList Exception:: com.aldorsolutions.webfdms.database.PersistenceException: PersistentObj:com.aldorsolutions.webfdms.beans.DbSpeedData Message: Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() or Connection.prepareStatement().

          at com.aldorsolutions.webfdms.database.DatabasePeer.insert(DatabasePeer.java:100)

          at com.aldorsolutions.webfdms.database.Persistent.save(Persistent.java:413)

          at com.aldorsolutions.webfdms.database.Transaction.save(Transaction.java:174)

          at com.aldorsolutions.webfdms.database.DatabaseTransaction.save(DatabaseTransaction.java:332)

          at com.aldorsolutions.webfdms.beans.FdmsDb.addSpeedDataRow(FdmsDb.java:239)

          at fdms.ui.struts.action.AddSpeedDataAction.execute(AddSpeedDataAction.java:55)

          at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)

          at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)

          at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)

          at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)

          at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)

          at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)

          at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)

          at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130)

          at fdms.ui.ActionTimmingFilter.doFilter(ActionTimmingFilter.java:91)

          at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)

          at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)

          at fdms.ui.SessionFilter.doFilter(SessionFilter.java:94)

          at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)

          at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)

          at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85)

          at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)

          at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)

          at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)

          at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

          at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)

          at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)

          at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

          at io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:51)

          at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)

          at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)

          at io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:56)

          at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)

          at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)

          at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)

          at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)

          at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

          at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)

          at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

          at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)

          at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)

          at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)

          at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)

          at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)

          at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)

          at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)

          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

          at java.lang.Thread.run(Thread.java:745)

          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

       

       

       

       

      Code:

       

       

              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);

                      }

         

         

      Code for 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);

          }

       

       

      I googled and found that I need to add

      `Statement.RETURN_GENERATED_KEYS` to the `connection.prepareStatement()`.

       

       

      But the code earlier use to run properly on JBoss 4.0.4.GA and I have not changed code at all. Only change is JBoss upgraded to Wildfly.

      I just wonder how does it happens.

       

       

      What may be the issue?

      Please help.