KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.PreparedStatement JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30
31 import javax.ejb.EJBException JavaDoc;
32
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.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
37 import org.jboss.ejb.EntityEnterpriseContext;
38 import org.jboss.deployment.DeploymentException;
39 import org.jboss.logging.Logger;
40
41 /**
42  * Create command for MySQL that uses the driver's getGeneratedKeys method
43  * to retrieve AUTO_INCREMENT values.
44  *
45  * @author <a HREF="mailto:jeremy@boynes.com">Jeremy Boynes</a>
46  * @author Scott.Stark@jboss.org
47  * @version $Revision: 37459 $
48  */

49 public class JDBCMySQLCreateCommand extends JDBCIdentityColumnCreateCommand
50 {
51    private static final Logger log = Logger.getLogger(JDBCMySQLCreateCommand.class);
52    private String JavaDoc className;
53    private String JavaDoc methodName;
54    private Method JavaDoc method;
55    private Method JavaDoc getUnderlyingStatement;
56
57    public void init(JDBCStoreManager manager) throws DeploymentException
58    {
59       super.init(manager);
60       ClassLoader JavaDoc loader = GetTCLAction.getContextClassLoader();
61       try
62       {
63          Class JavaDoc psClass = loader.loadClass(className);
64          method = psClass.getMethod(methodName, null);
65       }
66       catch (ClassNotFoundException JavaDoc e)
67       {
68          throw new DeploymentException("Could not load driver class: " + className, e);
69       }
70       catch (NoSuchMethodException JavaDoc e)
71       {
72          throw new DeploymentException("Driver does not have method: " + methodName + "()");
73       }
74
75       try
76       {
77          Class JavaDoc wrapperClass = loader.loadClass("org.jboss.resource.adapter.jdbc.StatementAccess");
78          getUnderlyingStatement = wrapperClass.getMethod("getUnderlyingStatement", null);
79       }
80       catch (ClassNotFoundException JavaDoc e)
81       {
82          throw new DeploymentException("Could not load org.jboss.resource.adapter.jdbc.StatementAccess", e);
83       }
84       catch (NoSuchMethodException JavaDoc e)
85       {
86          throw new DeploymentException("StatementAccess.getUnderlyingStatement not found", e);
87       }
88    }
89
90    protected void initEntityCommand(JDBCEntityCommandMetaData entityCommand) throws DeploymentException
91    {
92       super.initEntityCommand(entityCommand);
93       className = entityCommand.getAttribute("class-name");
94       if (className == null)
95       {
96          className = "com.mysql.jdbc.PreparedStatement";
97       }
98       methodName = entityCommand.getAttribute("method");
99       if (methodName == null)
100       {
101          methodName = "getGeneratedKeys";
102       }
103    }
104
105    protected int executeInsert(int paramIndex, PreparedStatement JavaDoc ps, EntityEnterpriseContext ctx) throws SQLException JavaDoc
106    {
107       int rows = ps.executeUpdate();
108
109       // remove any JCA wrappers
110
Statement JavaDoc stmt = ps;
111       do
112       {
113          try
114          {
115             Object JavaDoc[] args = {};
116             stmt = (Statement JavaDoc) getUnderlyingStatement.invoke(stmt, args);
117          }
118          catch (IllegalAccessException JavaDoc e)
119          {
120             SQLException JavaDoc ex = new SQLException JavaDoc("Failed to invoke getUnderlyingStatement");
121             ex.initCause(e);
122             throw ex;
123          }
124          catch (InvocationTargetException JavaDoc e)
125          {
126             SQLException JavaDoc ex = new SQLException JavaDoc("Failed to invoke getUnderlyingStatement");
127             ex.initCause(e);
128             throw ex;
129          }
130       } while (stmt != null && method.getDeclaringClass().isInstance(stmt) == false);
131
132       ResultSet JavaDoc rs = null;
133       try
134       {
135          rs = (ResultSet JavaDoc) method.invoke(stmt, null);
136          if (!rs.next())
137          {
138             throw new EJBException JavaDoc("getGeneratedKeys returned an empty ResultSet");
139          }
140          pkField.loadInstanceResults(rs, 1, ctx);
141       }
142       catch (RuntimeException JavaDoc e)
143       {
144          throw e;
145       }
146       catch (Exception JavaDoc e)
147       {
148          // throw EJBException to force a rollback as the row has been inserted
149
throw new EJBException JavaDoc("Error extracting generated keys", e);
150       }
151       finally
152       {
153          JDBCUtil.safeClose(rs);
154       }
155       return rows;
156    }
157 }
158
Popular Tags