1 22 package org.jboss.ejb.plugins.cmp.jdbc.keygen; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.SQLException ; 26 import java.sql.ResultSet ; 27 import java.sql.Statement ; 28 import java.lang.reflect.Method ; 29 import java.lang.reflect.InvocationTargetException ; 30 31 import javax.ejb.EJBException ; 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 49 public class JDBCMySQLCreateCommand extends JDBCIdentityColumnCreateCommand 50 { 51 private static final Logger log = Logger.getLogger(JDBCMySQLCreateCommand.class); 52 private String className; 53 private String methodName; 54 private Method method; 55 private Method getUnderlyingStatement; 56 57 public void init(JDBCStoreManager manager) throws DeploymentException 58 { 59 super.init(manager); 60 ClassLoader loader = GetTCLAction.getContextClassLoader(); 61 try 62 { 63 Class psClass = loader.loadClass(className); 64 method = psClass.getMethod(methodName, null); 65 } 66 catch (ClassNotFoundException e) 67 { 68 throw new DeploymentException("Could not load driver class: " + className, e); 69 } 70 catch (NoSuchMethodException e) 71 { 72 throw new DeploymentException("Driver does not have method: " + methodName + "()"); 73 } 74 75 try 76 { 77 Class wrapperClass = loader.loadClass("org.jboss.resource.adapter.jdbc.StatementAccess"); 78 getUnderlyingStatement = wrapperClass.getMethod("getUnderlyingStatement", null); 79 } 80 catch (ClassNotFoundException e) 81 { 82 throw new DeploymentException("Could not load org.jboss.resource.adapter.jdbc.StatementAccess", e); 83 } 84 catch (NoSuchMethodException 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 ps, EntityEnterpriseContext ctx) throws SQLException 106 { 107 int rows = ps.executeUpdate(); 108 109 Statement stmt = ps; 111 do 112 { 113 try 114 { 115 Object [] args = {}; 116 stmt = (Statement ) getUnderlyingStatement.invoke(stmt, args); 117 } 118 catch (IllegalAccessException e) 119 { 120 SQLException ex = new SQLException ("Failed to invoke getUnderlyingStatement"); 121 ex.initCause(e); 122 throw ex; 123 } 124 catch (InvocationTargetException e) 125 { 126 SQLException ex = new SQLException ("Failed to invoke getUnderlyingStatement"); 127 ex.initCause(e); 128 throw ex; 129 } 130 } while (stmt != null && method.getDeclaringClass().isInstance(stmt) == false); 131 132 ResultSet rs = null; 133 try 134 { 135 rs = (ResultSet ) method.invoke(stmt, null); 136 if (!rs.next()) 137 { 138 throw new EJBException ("getGeneratedKeys returned an empty ResultSet"); 139 } 140 pkField.loadInstanceResults(rs, 1, ctx); 141 } 142 catch (RuntimeException e) 143 { 144 throw e; 145 } 146 catch (Exception e) 147 { 148 throw new EJBException ("Error extracting generated keys", e); 150 } 151 finally 152 { 153 JDBCUtil.safeClose(rs); 154 } 155 return rows; 156 } 157 } 158 | Popular Tags |