[jboss-cvs] JBossAS SVN: r62395 - in trunk/server/src: main/org/jboss/ejb/plugins/cmp/jdbc2 and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 18 06:04:26 EDT 2007


Author: alex.loubyansky at jboss.com
Date: 2007-04-18 06:04:26 -0400 (Wed, 18 Apr 2007)
New Revision: 62395

Added:
   trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/
   trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/AbstractCreateCommand.java
   trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/HsqldbCreateCommand.java
   trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/PostgreSQLCreateCommand.java
Modified:
   trunk/server/src/etc/conf/default/standardjbosscmp-jdbc.xml
Log:
JBAS-4340 contributed by Jesper Pedersen

Modified: trunk/server/src/etc/conf/default/standardjbosscmp-jdbc.xml
===================================================================
--- trunk/server/src/etc/conf/default/standardjbosscmp-jdbc.xml	2007-04-18 10:04:08 UTC (rev 62394)
+++ trunk/server/src/etc/conf/default/standardjbosscmp-jdbc.xml	2007-04-18 10:04:26 UTC (rev 62395)
@@ -3029,6 +3029,22 @@
          <!-- change to define SQL used to obtain key prior to insert -->
          <attribute name="pk-sql">SELECT CURRENT_TIMESTAMP</attribute>
       </entity-command>
+
+      <!-- retrieves generated key of the record inserted into hsql db for jdbc2 pm -->
+      <entity-command name="jdbc2pm-hsqldb-fetch-key"
+                      class="org.jboss.ejb.plugins.cmp.jdbc2.keygen.HsqldbCreateCommand">
+         <!-- uncomment to change SQL statement used to obtain identity
+         <attribute name="pk-sql">CALL IDENTITY()</attribute>
+         -->
+      </entity-command>
+
+      <!-- retrieves the currval of the the sequence associated with a PostgreSQL SERIAL column for jdbc2 pm -->
+      <entity-command name="jdbc2pm-postgresql-fetch-seq"
+                      class="org.jboss.ejb.plugins.cmp.jdbc2.keygen.PostgreSQLCreateCommand">
+         <!-- uncomment to change the name of the sequence; default is ${table}_${pkColumn}_seq
+         <attribute name="sequence"></attribute>
+         -->
+      </entity-command>
    </entity-commands>
 
    <!-- reserved words that should not be used as table names -->

Added: trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/AbstractCreateCommand.java
===================================================================
--- trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/AbstractCreateCommand.java	                        (rev 0)
+++ trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/AbstractCreateCommand.java	2007-04-18 10:04:26 UTC (rev 62395)
@@ -0,0 +1,148 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb.plugins.cmp.jdbc2.keygen;
+
+import org.jboss.deployment.DeploymentException;
+import org.jboss.ejb.EntityEnterpriseContext;
+import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
+import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
+import org.jboss.ejb.plugins.cmp.jdbc2.CreateCommand;
+import org.jboss.ejb.plugins.cmp.jdbc2.JDBCStoreManager2;
+import org.jboss.ejb.plugins.cmp.jdbc2.PersistentContext;
+import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCCMPFieldBridge2;
+import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2;
+import org.jboss.logging.Logger;
+
+import java.lang.reflect.Method;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import javax.ejb.CreateException;
+import javax.ejb.DuplicateKeyException;
+
+/**
+ * Abstract create command
+ * @author <a href="mailto:jep at worldleaguesports.com">Jesper Pedersen</a>
+ * @version <tt>$Revision: 57209 $</tt>
+ */
+public abstract class AbstractCreateCommand
+   implements CreateCommand
+{
+   protected JDBCEntityBridge2 entityBridge;
+   protected Logger log;
+   protected JDBCCMPFieldBridge2 pkField;
+   protected String pkSql;
+
+   public void init(JDBCStoreManager2 manager) throws DeploymentException
+   {
+      this.entityBridge = (JDBCEntityBridge2)manager.getEntityBridge();
+      this.log = Logger.getLogger(getClass().getName() + "." + entityBridge.getEntityName());
+
+      final JDBCFieldBridge[] pkFields = entityBridge.getPrimaryKeyFields();
+      if(pkFields.length > 1)
+      {
+         throw new DeploymentException("This entity-command cannot be used with composite primary keys!");
+      }
+
+      this.pkField = (JDBCCMPFieldBridge2) pkFields[0];
+      this.pkSql = "";
+   }
+
+   public Object execute(Method m, Object[] args, EntityEnterpriseContext ctx) throws CreateException
+   {
+      Object pk;
+      PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
+      if(ctx.getId() == null)
+      {
+         Connection con = null;
+         PreparedStatement ps = null;
+         ResultSet rs = null;
+         try
+         {
+            if(log.isDebugEnabled())
+            {
+               log.debug("executing sql: " + pkSql);
+            }
+
+            con = entityBridge.getDataSource().getConnection();
+            ps = con.prepareStatement(pkSql);
+            rs = ps.executeQuery();
+
+            if(!rs.next())
+            {
+               throw new CreateException("pk-sql " + pkSql + " returned no results!");
+            }
+
+            pk = pkField.loadArgumentResults(rs, 1);
+            pctx.setFieldValue(pkField.getRowIndex(), pk);
+            pk = entityBridge.extractPrimaryKeyFromInstance(ctx);
+         }
+         catch(SQLException e)
+         {
+            log.error("Failed to execute pk sql. error code: " + e.getErrorCode() + ", sql state: " + e.getSQLState(), e);
+            throw new CreateException("Failed to execute pk sql: " + e.getMessage() +
+               ", error code: " + e.getErrorCode() + ", sql state: " + e.getSQLState());
+         }
+         finally
+         {
+            JDBCUtil.safeClose(rs);
+            JDBCUtil.safeClose(ps);
+            JDBCUtil.safeClose(con);
+         }
+
+         if(pk == null)
+         {
+            log.error("Primary key for created instance is null.");
+            throw new CreateException("Primary key for created instance is null.");
+         }
+
+         pctx.setPk(pk);
+      }
+      else
+      {
+         // insert-after-ejb-post-create
+         try
+         {
+            pctx.flush();
+         }
+         catch(SQLException e)
+         {
+            if("23000".equals(e.getSQLState()))
+            {
+               throw new DuplicateKeyException("Unique key violation or invalid foreign key value: pk=" + ctx.getId());
+            }
+            else
+            {
+               throw new CreateException("Failed to create instance: pk=" +
+                  ctx.getId() +
+                  ", state=" +
+                  e.getSQLState() +
+                  ", msg=" + e.getMessage());
+            }
+         }
+         pk = ctx.getId();
+      }
+      return pk;
+   }
+}

Added: trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/HsqldbCreateCommand.java
===================================================================
--- trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/HsqldbCreateCommand.java	                        (rev 0)
+++ trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/HsqldbCreateCommand.java	2007-04-18 10:04:26 UTC (rev 62395)
@@ -0,0 +1,55 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb.plugins.cmp.jdbc2.keygen;
+
+import org.jboss.deployment.DeploymentException;
+import org.jboss.ejb.plugins.cmp.jdbc.SQLUtil;
+import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
+import org.jboss.ejb.plugins.cmp.jdbc2.JDBCStoreManager2;
+
+/**
+ * Create command for Hypersonic that generated keys using an IDENTITY column.
+ *
+ * @author <a href="mailto:jep at worldleaguesports.com">Jesper Pedersen</a>
+ * @version $Revision: 57209 $
+ */
+public class HsqldbCreateCommand
+   extends AbstractCreateCommand
+{
+
+   public void init(JDBCStoreManager2 manager) throws DeploymentException
+   {
+      super.init(manager);
+
+      JDBCEntityCommandMetaData metadata = entityBridge.getMetaData().getEntityCommand();
+      pkSql = metadata.getAttribute("pk-sql");
+      if(pkSql == null)
+      {
+         pkSql = "CALL IDENTITY()";
+      }
+
+      if(log.isDebugEnabled())
+      {
+         log.debug("entity-command generate pk sql: " + pkSql);
+      }
+   }
+}

Added: trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/PostgreSQLCreateCommand.java
===================================================================
--- trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/PostgreSQLCreateCommand.java	                        (rev 0)
+++ trunk/server/src/main/org/jboss/ejb/plugins/cmp/jdbc2/keygen/PostgreSQLCreateCommand.java	2007-04-18 10:04:26 UTC (rev 62395)
@@ -0,0 +1,57 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb.plugins.cmp.jdbc2.keygen;
+
+import org.jboss.deployment.DeploymentException;
+import org.jboss.ejb.plugins.cmp.jdbc.SQLUtil;
+import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
+import org.jboss.ejb.plugins.cmp.jdbc2.JDBCStoreManager2;
+
+/**
+ * PostgreSQL create command
+ * @author <a href="mailto:jep at worldleaguesports.com">Jesper Pedersen</a>
+ * @version <tt>$Revision: 57209 $</tt>
+ */
+public class PostgreSQLCreateCommand
+   extends AbstractCreateCommand
+{
+
+   public void init(JDBCStoreManager2 manager) throws DeploymentException
+   {
+      super.init(manager);
+
+      JDBCEntityCommandMetaData metadata = entityBridge.getMetaData().getEntityCommand();
+      String sequence = metadata.getAttribute("sequence");
+      if (sequence == null) {
+         sequence = entityBridge.getQualifiedTableName()
+            + '_' + SQLUtil.getColumnNamesClause(pkField, new StringBuffer(20))
+            + "_seq";
+      }
+
+      pkSql = "SELECT currval('" + sequence + "')";
+
+      if(log.isDebugEnabled())
+      {
+         log.debug("entity-command generate pk sql: " + pkSql);
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list