1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.sql.Connection ; 25 import java.sql.DatabaseMetaData ; 26 import java.sql.ResultSet ; 27 import java.sql.Statement ; 28 import java.sql.SQLException ; 29 import javax.sql.DataSource ; 30 import javax.transaction.Transaction ; 31 import javax.transaction.TransactionManager ; 32 33 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCAbstractCMRFieldBridge; 34 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCAbstractEntityBridge; 35 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityMetaData; 36 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCRelationMetaData; 37 import org.jboss.logging.Logger; 38 39 40 49 public final class JDBCStopCommand 50 { 51 private final JDBCEntityPersistenceStore manager; 52 private final JDBCAbstractEntityBridge entity; 53 private final JDBCEntityMetaData entityMetaData; 54 private final Logger log; 55 56 public JDBCStopCommand(JDBCEntityPersistenceStore manager) 57 { 58 this.manager = manager; 59 entity = manager.getEntityBridge(); 60 entityMetaData = entity.getMetaData(); 61 62 log = Logger.getLogger( 64 this.getClass().getName() + 65 "." + 66 manager.getMetaData().getName()); 67 } 68 69 public boolean execute() 70 { 71 boolean success = true; 72 73 JDBCAbstractCMRFieldBridge[] cmrFields = entity.getCMRFields(); 75 for(int i = 0; i < cmrFields.length; ++i) 76 { 77 JDBCAbstractCMRFieldBridge cmrField = cmrFields[i]; 78 JDBCRelationMetaData relationMetaData = cmrField.getMetaData().getRelationMetaData(); 79 if(relationMetaData.isTableMappingStyle() && !relationMetaData.isTableDropped()) 80 { 81 if(relationMetaData.getRemoveTable()) 82 { 83 final boolean dropped = dropTable(relationMetaData.getDataSource(), cmrField.getQualifiedTableName()); 84 if(!dropped) 85 { 86 success = false; 87 } 88 else 89 { 90 relationMetaData.setTableDropped(); 91 } 92 } 93 } 94 } 95 96 if(entityMetaData.getRemoveTable()) 97 { 98 boolean dropped = dropTable(entity.getDataSource(), entity.getQualifiedTableName()); 99 if(!dropped) 100 { 101 success = false; 102 } 103 } 104 105 return success; 106 } 107 108 private boolean dropTable(DataSource dataSource, String qualifiedTableName) 109 { 110 boolean success; 111 Connection con = null; 112 ResultSet rs = null; 113 114 String schema = SQLUtil.getSchema(qualifiedTableName); 115 String tableName = schema != null ? SQLUtil.getTableNameWithoutSchema(qualifiedTableName) : qualifiedTableName; 116 117 try 119 { 120 con = dataSource.getConnection(); 121 DatabaseMetaData dmd = con.getMetaData(); 122 rs = dmd.getTables(con.getCatalog(), schema, tableName, null); 123 if(!rs.next()) 124 { 125 return true; 126 } 127 } 128 catch(SQLException e) 129 { 130 log.debug("Error getting database metadata for DROP TABLE command. " + 131 " DROP TABLE will not be executed. ", e); 132 return true; 133 } 134 finally 135 { 136 JDBCUtil.safeClose(rs); 137 JDBCUtil.safeClose(con); 138 } 139 140 142 TransactionManager tm = manager.getContainer().getTransactionManager(); 144 Transaction oldTransaction = null; 145 try 146 { 147 oldTransaction = tm.suspend(); 148 } 149 catch(Exception e) 150 { 151 log.error("Could not suspend current transaction before drop table. " + 152 "'" + qualifiedTableName + "' will not be dropped.", e); 153 } 154 155 try 156 { 157 Statement statement = null; 158 try 159 { 160 con = dataSource.getConnection(); 161 statement = con.createStatement(); 162 163 String sql = SQLUtil.DROP_TABLE + qualifiedTableName; 165 log.debug("Executing SQL: " + sql); 166 statement.executeUpdate(sql); 167 success = true; 168 } 169 finally 170 { 171 JDBCUtil.safeClose(statement); 172 JDBCUtil.safeClose(con); 173 } 174 } 175 catch(Exception e) 176 { 177 log.debug("Could not drop table " + qualifiedTableName + ": " + e.getMessage()); 178 success = false; 179 } 180 finally 181 { 182 try 183 { 184 if(oldTransaction != null) 186 { 187 tm.resume(oldTransaction); 188 } 189 } 190 catch(Exception e) 191 { 192 log.error("Could not reattach original transaction after drop table"); 193 } 194 } 195 196 return success; 197 } 198 } 199 | Popular Tags |