001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc.keygen;
023:
024: import java.sql.PreparedStatement;
025: import java.sql.SQLException;
026: import java.sql.ResultSet;
027: import java.sql.Statement;
028: import java.lang.reflect.Method;
029: import java.lang.reflect.InvocationTargetException;
030:
031: import javax.ejb.EJBException;
032:
033: import org.jboss.ejb.plugins.cmp.jdbc.JDBCIdentityColumnCreateCommand;
034: import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
035: import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
036: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
037: import org.jboss.ejb.EntityEnterpriseContext;
038: import org.jboss.deployment.DeploymentException;
039: import org.jboss.logging.Logger;
040:
041: /**
042: * Create command for MySQL that uses the driver's getGeneratedKeys method
043: * to retrieve AUTO_INCREMENT values.
044: *
045: * @author <a href="mailto:jeremy@boynes.com">Jeremy Boynes</a>
046: * @author Scott.Stark@jboss.org
047: * @version $Revision: 57209 $
048: */
049: public class JDBCMySQLCreateCommand extends
050: JDBCIdentityColumnCreateCommand {
051: private static final Logger log = Logger
052: .getLogger(JDBCMySQLCreateCommand.class);
053: private String className;
054: private String methodName;
055: private Method method;
056: private Method getUnderlyingStatement;
057:
058: public void init(JDBCStoreManager manager)
059: throws DeploymentException {
060: super .init(manager);
061: ClassLoader loader = GetTCLAction.getContextClassLoader();
062: try {
063: Class psClass = loader.loadClass(className);
064: method = psClass.getMethod(methodName, null);
065: } catch (ClassNotFoundException e) {
066: throw new DeploymentException(
067: "Could not load driver class: " + className, e);
068: } catch (NoSuchMethodException e) {
069: throw new DeploymentException(
070: "Driver does not have method: " + methodName + "()");
071: }
072:
073: try {
074: Class wrapperClass = loader
075: .loadClass("org.jboss.resource.adapter.jdbc.StatementAccess");
076: getUnderlyingStatement = wrapperClass.getMethod(
077: "getUnderlyingStatement", null);
078: } catch (ClassNotFoundException e) {
079: throw new DeploymentException(
080: "Could not load org.jboss.resource.adapter.jdbc.StatementAccess",
081: e);
082: } catch (NoSuchMethodException e) {
083: throw new DeploymentException(
084: "StatementAccess.getUnderlyingStatement not found",
085: e);
086: }
087: }
088:
089: protected void initEntityCommand(
090: JDBCEntityCommandMetaData entityCommand)
091: throws DeploymentException {
092: super .initEntityCommand(entityCommand);
093: className = entityCommand.getAttribute("class-name");
094: if (className == null) {
095: className = "com.mysql.jdbc.PreparedStatement";
096: }
097: methodName = entityCommand.getAttribute("method");
098: if (methodName == null) {
099: methodName = "getGeneratedKeys";
100: }
101: }
102:
103: protected int executeInsert(int paramIndex, PreparedStatement ps,
104: EntityEnterpriseContext ctx) throws SQLException {
105: int rows = ps.executeUpdate();
106:
107: // remove any JCA wrappers
108: Statement stmt = ps;
109: do {
110: try {
111: Object[] args = {};
112: stmt = (Statement) getUnderlyingStatement.invoke(stmt,
113: args);
114: } catch (IllegalAccessException e) {
115: SQLException ex = new SQLException(
116: "Failed to invoke getUnderlyingStatement");
117: ex.initCause(e);
118: throw ex;
119: } catch (InvocationTargetException e) {
120: SQLException ex = new SQLException(
121: "Failed to invoke getUnderlyingStatement");
122: ex.initCause(e);
123: throw ex;
124: }
125: } while (stmt != null
126: && method.getDeclaringClass().isInstance(stmt) == false);
127:
128: ResultSet rs = null;
129: try {
130: rs = (ResultSet) method.invoke(stmt, null);
131: if (!rs.next()) {
132: throw new EJBException(
133: "getGeneratedKeys returned an empty ResultSet");
134: }
135: pkField.loadInstanceResults(rs, 1, ctx);
136: } catch (RuntimeException e) {
137: throw e;
138: } catch (Exception e) {
139: // throw EJBException to force a rollback as the row has been inserted
140: throw new EJBException("Error extracting generated keys", e);
141: } finally {
142: JDBCUtil.safeClose(rs);
143: }
144: return rows;
145: }
146: }
|