1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import java.sql.SQLException ; 26 import java.sql.PreparedStatement ; 27 import java.sql.Connection ; 28 import java.sql.Statement ; 29 import java.sql.ResultSet ; 30 31 import javax.ejb.EJBException ; 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 45 public abstract class JDBCIdentityColumnCreateCommand extends JDBCAbstractCreateCommand 46 { 47 protected JDBCCMPFieldBridge pkField; 48 protected String pkSQL; 49 50 protected boolean isInsertField(JDBCFieldBridge field) 51 { 52 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 ps, EntityEnterpriseContext ctx) throws SQLException 63 { 64 int rows = ps.executeUpdate(); 65 Connection c; 66 Statement s = null; 67 ResultSet 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 ("ResultSet was empty"); 74 } 75 pkField.loadInstanceResults(rs, 1, ctx); 76 } catch (RuntimeException e) { 77 throw e; 78 } catch (Exception e) { 79 throw new EJBException ("Error extracting generated key", e); 81 } finally { 82 JDBCUtil.safeClose(rs); 83 JDBCUtil.safeClose(s); 84 } 85 return rows; 86 } 87 88 93 protected SQLException processException(Throwable t) { 94 if (t instanceof InvocationTargetException ) { 95 t = ((InvocationTargetException ) t).getTargetException(); 96 } 97 if (t instanceof SQLException ) { 98 return (SQLException ) t; 99 } 100 if (t instanceof RuntimeException ) { 101 throw (RuntimeException ) t; 102 } 103 if (t instanceof Error ) { 104 throw (Error ) t; 105 } 106 log.error(t); 107 throw new IllegalStateException (); 108 } 109 } 110 | Popular Tags |