KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > keygen > JDBC30GeneratedKeysCreateCommand


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.keygen;
23
24 import java.lang.reflect.Field JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.sql.Connection JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31
32 import org.jboss.deployment.DeploymentException;
33 import org.jboss.ejb.plugins.cmp.jdbc.JDBCIdentityColumnCreateCommand;
34 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
35 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
36 import org.jboss.ejb.EntityEnterpriseContext;
37
38 /**
39  * Create method that uses the JDBC 3.0 getGeneratedKeys method to obtain
40  * the value from the identity column.
41  *
42  * @author <a HREF="mailto:jeremy@boynes.com">Jeremy Boynes</a>
43  * @version $Revision: 37459 $
44  */

45 public class JDBC30GeneratedKeysCreateCommand extends JDBCIdentityColumnCreateCommand
46 {
47    private static final Method JavaDoc CONNECTION_PREPARE;
48    private static final Integer JavaDoc GENERATE_KEYS;
49    private static final Method JavaDoc GET_GENERATED_KEYS;
50    static {
51       Method JavaDoc prepare, getGeneratedKeys;
52       Integer JavaDoc generateKeys;
53       try {
54          prepare = Connection JavaDoc.class.getMethod("prepareStatement", new Class JavaDoc[] {String JavaDoc.class, int.class});
55          getGeneratedKeys = PreparedStatement JavaDoc.class.getMethod("getGeneratedKeys", null);
56          Field JavaDoc f = PreparedStatement JavaDoc.class.getField("RETURN_GENERATED_KEYS");
57          generateKeys = (Integer JavaDoc) f.get(PreparedStatement JavaDoc.class);
58       } catch (Exception JavaDoc e) {
59          prepare = null;
60          getGeneratedKeys = null;
61          generateKeys = null;
62       }
63       CONNECTION_PREPARE = prepare;
64       GET_GENERATED_KEYS = getGeneratedKeys;
65       GENERATE_KEYS = generateKeys;
66    }
67
68    public void init(JDBCStoreManager manager) throws DeploymentException
69    {
70       if (CONNECTION_PREPARE == null) {
71          throw new DeploymentException("Create command requires JDBC 3.0 (JDK1.4+)");
72       }
73       super.init(manager);
74    }
75
76    protected PreparedStatement JavaDoc prepareStatement(Connection JavaDoc c, String JavaDoc sql, EntityEnterpriseContext ctx) throws SQLException JavaDoc
77    {
78       try {
79          return (PreparedStatement JavaDoc) CONNECTION_PREPARE.invoke(c, new Object JavaDoc[] { sql, GENERATE_KEYS });
80       } catch (Exception JavaDoc e) {
81          throw processException(e);
82       }
83    }
84
85    protected int executeInsert(int paramIndex, PreparedStatement JavaDoc ps, EntityEnterpriseContext ctx) throws SQLException JavaDoc
86    {
87       int rows = ps.executeUpdate();
88       ResultSet JavaDoc rs = null;
89       try {
90          rs = (ResultSet JavaDoc) GET_GENERATED_KEYS.invoke(ps, null);
91          if (!rs.next()) {
92             // throw EJBException to force a rollback as the row has been inserted
93
throw new EJBException JavaDoc("getGeneratedKeys returned an empty ResultSet");
94          }
95          pkField.loadInstanceResults(rs, 1, ctx);
96       } catch (RuntimeException JavaDoc e) {
97          throw e;
98       } catch (Exception JavaDoc e) {
99          // throw EJBException to force a rollback as the row has been inserted
100
throw new EJBException JavaDoc("Error extracting generated keys", e);
101       } finally {
102          JDBCUtil.safeClose(rs);
103       }
104       return rows;
105    }
106 }
107
Popular Tags