Uploaded image for project: 'Application Server 3  4  5 and 6'
  1. Application Server 3 4 5 and 6
  2. JBAS-2399

Enhancement to get-generated-keys entity command

XMLWordPrintable

    • Icon: Patch Patch
    • Resolution: Obsolete
    • Icon: Major Major
    • No Release
    • None
    • CMP service
    • None

      The get-generated-keys entity command currently assumes that whatever the database driver returns for generated keys by default will match the primary key of the entity bean. Databases like Oracle that implement a hidden unique identifier (RowID) tend to return this as the generated key. This doesn't work when your real PK is a number generated by a database trigger. It also doesn't work when the PK class spans multiple columns. This patch changes how the prepared statement is created, passing a list of columns to be returned by the driver instead of just telling the driver to return its default value(s). The columns that are passed are the ones that make up the PK.


      Patch generated via cygwin "diff -b -u old new" from 4.0.3SP1 src.

      — JDBC30GeneratedKeysCreateCommand.java 2005-09-12 21:02:32.000000000 -0400
      +++ JDBC30GeneratedKeysCreateCommand.java.new 2005-10-25 17:25:30.000000000 -0400
      @@ -6,7 +6,6 @@
      */
      package org.jboss.ejb.plugins.cmp.jdbc.keygen;

      -import java.lang.reflect.Field;
      import java.lang.reflect.Method;
      import java.sql.Connection;
      import java.sql.PreparedStatement;
      @@ -30,24 +29,19 @@
      public class JDBC30GeneratedKeysCreateCommand extends JDBCIdentityColumnCreateCommand
      {
      private static final Method CONNECTION_PREPARE;

      • private static final Integer GENERATE_KEYS;
        private static final Method GET_GENERATED_KEYS;
        static {
        Method prepare, getGeneratedKeys;
        Integer generateKeys;
        try {
      • prepare = Connection.class.getMethod("prepareStatement", new Class[] {String.class, int.class}

        );
        + prepare = Connection.class.getMethod("prepareStatement", new Class[]

        {String.class, String[].class}

        );
        getGeneratedKeys = PreparedStatement.class.getMethod("getGeneratedKeys", null);

      • Field f = PreparedStatement.class.getField("RETURN_GENERATED_KEYS");
      • generateKeys = (Integer) f.get(PreparedStatement.class);
        } catch (Exception e) { prepare = null; getGeneratedKeys = null; - generateKeys = null; }

        CONNECTION_PREPARE = prepare;
        GET_GENERATED_KEYS = getGeneratedKeys;

      • GENERATE_KEYS = generateKeys;
        }

      public void init(JDBCStoreManager manager) throws DeploymentException
      @@ -61,7 +55,7 @@
      protected PreparedStatement prepareStatement(Connection c, String sql, EntityEnterpriseContext ctx) throws SQLException
      {
      try {

      • return (PreparedStatement) CONNECTION_PREPARE.invoke(c, new Object[] { sql, GENERATE_KEYS }

        );
        + return (PreparedStatement) CONNECTION_PREPARE.invoke(c, new Object[]

        { sql, pkField.getJDBCType().getColumnNames() }

        );
        } catch (Exception e)

        { throw processException(e); }

              olubyans@redhat.com Alexey Loubyansky
              sarnoth_jira Jesse Sterr (Inactive)
              Votes:
              1 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved: