KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCIdentityColumnCreateCommand


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30
31 import javax.ejb.EJBException JavaDoc;
32
33 import org.jboss.deployment.DeploymentException;
34 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
35 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
36 import org.jboss.ejb.EntityEnterpriseContext;
37
38 /**
39  * Base class for create commands where the PK value is generated as side
40  * effect of performing the insert operation. This is typically associated
41  * with database platforms that use identity columns
42  *
43  * @author <a HREF="mailto:jeremy@boynes.com">Jeremy Boynes</a>
44  */

45 public abstract class JDBCIdentityColumnCreateCommand extends JDBCAbstractCreateCommand
46 {
47    protected JDBCCMPFieldBridge pkField;
48    protected String JavaDoc pkSQL;
49
50    protected boolean isInsertField(JDBCFieldBridge field)
51    {
52       // do not include PK fields in the insert
53
return super.isInsertField(field) && !field.isPrimaryKeyMember();
54    }
55
56    protected void initGeneratedFields() throws DeploymentException
57    {
58       super.initGeneratedFields();
59       pkField = getGeneratedPKField();
60    }
61
62    protected int executeInsert(int paramIndex, PreparedStatement JavaDoc ps, EntityEnterpriseContext ctx) throws SQLException JavaDoc
63    {
64       int rows = ps.executeUpdate();
65       Connection JavaDoc c;
66       Statement JavaDoc s = null;
67       ResultSet JavaDoc rs = null;
68       try {
69          c = ps.getConnection();
70          s = c.createStatement();
71          rs = s.executeQuery(pkSQL);
72          if (!rs.next()) {
73             throw new EJBException JavaDoc("ResultSet was empty");
74          }
75          pkField.loadInstanceResults(rs, 1, ctx);
76       } catch (RuntimeException JavaDoc e) {
77          throw e;
78       } catch (Exception JavaDoc e) {
79          // throw EJBException to force a rollback as the row has been inserted
80
throw new EJBException JavaDoc("Error extracting generated key", e);
81       } finally {
82          JDBCUtil.safeClose(rs);
83          JDBCUtil.safeClose(s);
84       }
85       return rows;
86    }
87
88    /**
89     * Helper for subclasses that use reflection to avoid driver dependencies.
90     * @param t an Exception raised by a reflected call
91     * @return SQLException extracted from the Throwable
92     */

93    protected SQLException JavaDoc processException(Throwable JavaDoc t) {
94       if (t instanceof InvocationTargetException JavaDoc) {
95          t = ((InvocationTargetException JavaDoc) t).getTargetException();
96       }
97       if (t instanceof SQLException JavaDoc) {
98          return (SQLException JavaDoc) t;
99       }
100       if (t instanceof RuntimeException JavaDoc) {
101          throw (RuntimeException JavaDoc) t;
102       }
103       if (t instanceof Error JavaDoc) {
104          throw (Error JavaDoc) t;
105       }
106       log.error(t);
107       throw new IllegalStateException JavaDoc();
108    }
109 }
110
Popular Tags