1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.util.Map ; 27 import java.rmi.RemoteException ; 28 import javax.ejb.RemoveException ; 29 30 import org.jboss.ejb.EntityEnterpriseContext; 31 import org.jboss.ejb.EntityContainer; 32 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge; 33 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge; 34 import org.jboss.logging.Logger; 35 import org.jboss.metadata.ConfigurationMetaData; 36 import org.jboss.deployment.DeploymentException; 37 38 39 50 public final class JDBCRemoveEntityCommand 51 { 52 private final JDBCStoreManager manager; 53 private final JDBCEntityBridge entity; 54 private final Logger log; 55 private final String removeEntitySQL; 56 private final boolean syncOnCommitOnly; 57 private boolean batchCascadeDelete; 58 59 public JDBCRemoveEntityCommand(JDBCStoreManager manager) 60 throws DeploymentException 61 { 62 this.manager = manager; 63 entity = (JDBCEntityBridge) manager.getEntityBridge(); 64 65 log = Logger.getLogger( 67 this.getClass().getName() + 68 "." + 69 manager.getMetaData().getName()); 70 71 StringBuffer sql = new StringBuffer (); 72 sql.append(SQLUtil.DELETE_FROM) 73 .append(entity.getQualifiedTableName()) 74 .append(SQLUtil.WHERE); 75 SQLUtil.getWhereClause(entity.getPrimaryKeyFields(), sql); 76 77 removeEntitySQL = sql.toString(); 78 if(log.isDebugEnabled()) 79 log.debug("Remove SQL: " + removeEntitySQL); 80 81 ConfigurationMetaData containerConfig = manager.getContainer(). 82 getBeanMetaData().getContainerConfiguration(); 83 syncOnCommitOnly = containerConfig.getSyncOnCommitOnly(); 84 85 JDBCCMRFieldBridge[] cmrFields = (JDBCCMRFieldBridge[]) entity.getCMRFields(); 86 for(int i = 0; i < cmrFields.length; ++i) 87 { 88 if(cmrFields[i].isBatchCascadeDelete()) 89 { 90 batchCascadeDelete = true; 91 break; 92 } 93 } 94 } 95 96 public void execute(EntityEnterpriseContext ctx) 97 throws RemoveException , RemoteException 98 { 99 if(entity.isRemoved(ctx)) 100 { 101 throw new IllegalStateException ("Instance was already removed: id=" + ctx.getId()); 102 } 103 104 entity.setIsBeingRemoved(ctx); 105 106 Object [] oldRelationsRef = new Object [1]; 108 boolean needsSync = entity.removeFromRelations(ctx, oldRelationsRef); 109 110 if(!syncOnCommitOnly && needsSync) 113 { 114 EntityContainer.synchronizeEntitiesWithinTransaction(ctx.getTransaction()); 115 } 116 117 if(!batchCascadeDelete) 118 { 119 if(!entity.isScheduledForBatchCascadeDelete(ctx)) 120 { 121 executeDeleteSQL(ctx); 122 } 123 else 124 { 125 if(log.isTraceEnabled()) 126 log.trace("Instance is scheduled for cascade delete. id=" + ctx.getId()); 127 } 128 } 129 130 if(oldRelationsRef[0] != null) 132 { 133 Map oldRelations = (Map )oldRelationsRef[0]; 134 entity.cascadeDelete(ctx, oldRelations); 135 } 136 137 if(batchCascadeDelete) 138 { 139 if(!entity.isScheduledForBatchCascadeDelete(ctx)) 140 { 141 executeDeleteSQL(ctx); 142 } 143 else 144 { 145 if(log.isTraceEnabled()) 146 log.debug("Instance is scheduled for cascade delete. id=" + ctx.getId()); 147 } 148 } 149 150 entity.setRemoved(ctx); 151 manager.getReadAheadCache().removeCachedData(ctx.getId()); 152 } 153 154 private void executeDeleteSQL(EntityEnterpriseContext ctx) throws RemoveException 155 { 156 Object key = ctx.getId(); 157 Connection con = null; 158 PreparedStatement ps = null; 159 int rowsAffected = 0; 160 try 161 { 162 if(log.isDebugEnabled()) 163 log.debug("Executing SQL: " + removeEntitySQL); 164 165 con = entity.getDataSource().getConnection(); 167 ps = con.prepareStatement(removeEntitySQL); 168 169 entity.setPrimaryKeyParameters(ps, 1, key); 171 172 rowsAffected = ps.executeUpdate(); 174 } 175 catch(Exception e) 176 { 177 log.error("Could not remove " + key, e); 178 throw new RemoveException ("Could not remove " + key + ": " + e.getMessage()); 179 } 180 finally 181 { 182 JDBCUtil.safeClose(ps); 183 JDBCUtil.safeClose(con); 184 } 185 186 if(rowsAffected == 0) 188 { 189 log.error("Could not remove entity " + key); 190 throw new RemoveException ("Could not remove entity"); 191 } 192 193 if(log.isTraceEnabled()) 194 log.trace("Remove: Rows affected = " + rowsAffected); 195 } 196 } | Popular Tags |