[jboss-svn-commits] JBL Code SVN: r30093 - labs/jbosstm/workspace/whitingjr/trunk/performance/src/main/java/org/jboss/jbossts/performance/persistence/vendor.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Nov 10 12:25:07 EST 2009


Author: whitingjr
Date: 2009-11-10 12:25:06 -0500 (Tue, 10 Nov 2009)
New Revision: 30093

Added:
   labs/jbosstm/workspace/whitingjr/trunk/performance/src/main/java/org/jboss/jbossts/performance/persistence/vendor/AbstractConstraint.java
   labs/jbosstm/workspace/whitingjr/trunk/performance/src/main/java/org/jboss/jbossts/performance/persistence/vendor/OracleConstraint.java
Log:
Added support classes for constraint relaxing.

Added: labs/jbosstm/workspace/whitingjr/trunk/performance/src/main/java/org/jboss/jbossts/performance/persistence/vendor/AbstractConstraint.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/performance/src/main/java/org/jboss/jbossts/performance/persistence/vendor/AbstractConstraint.java	                        (rev 0)
+++ labs/jbosstm/workspace/whitingjr/trunk/performance/src/main/java/org/jboss/jbossts/performance/persistence/vendor/AbstractConstraint.java	2009-11-10 17:25:06 UTC (rev 30093)
@@ -0,0 +1,35 @@
+ /*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * This is free software; you can redistribute it and/or modify it
+  * under the terms of the GNU Lesser General Public License as
+  * published by the Free Software Foundation; either version 2.1 of
+  * the License, or (at your option) any later version.
+  *
+  * This software is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
+  * License along with this software; if not, write to the Free
+  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+  */
+
+package org.jboss.jbossts.performance.persistence.vendor;
+
+import java.util.Set;
+
+public abstract class AbstractConstraint implements IntegrityConstraint
+{
+   protected Set<String> tableNames;
+   public void setTableNames(Set<String> names)
+   {
+      this.tableNames = names;
+   }
+   
+}

Added: labs/jbosstm/workspace/whitingjr/trunk/performance/src/main/java/org/jboss/jbossts/performance/persistence/vendor/OracleConstraint.java
===================================================================
--- labs/jbosstm/workspace/whitingjr/trunk/performance/src/main/java/org/jboss/jbossts/performance/persistence/vendor/OracleConstraint.java	                        (rev 0)
+++ labs/jbosstm/workspace/whitingjr/trunk/performance/src/main/java/org/jboss/jbossts/performance/persistence/vendor/OracleConstraint.java	2009-11-10 17:25:06 UTC (rev 30093)
@@ -0,0 +1,143 @@
+ /*
+  * JBoss, Home of Professional Open Source
+  * Copyright 2005, JBoss Inc., and individual contributors as indicated
+  * by the @authors tag. See the copyright.txt in the distribution for a
+  * full listing of individual contributors.
+  *
+  * This is free software; you can redistribute it and/or modify it
+  * under the terms of the GNU Lesser General Public License as
+  * published by the Free Software Foundation; either version 2.1 of
+  * the License, or (at your option) any later version.
+  *
+  * This software is distributed in the hope that it will be useful,
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  * Lesser General Public License for more details.
+  *
+  * You should have received a copy of the GNU Lesser General Public
+  * License along with this software; if not, write to the Free
+  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+  */
+
+package org.jboss.jbossts.performance.persistence.vendor;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.dbutils.DbUtils;
+import org.apache.log4j.Logger;
+
+public class OracleConstraint extends AbstractConstraint
+{
+   private static final Logger logger = Logger.getLogger(OracleConstraint.class);
+   private Map<String, List<String>> disabled; // keyed by table, listed by constraint
+   
+   @Override
+   public void constrain(Connection connection) throws SQLException
+   {
+      if (null != disabled && !disabled.isEmpty())
+      {
+         for (String table : this.disabled.keySet())
+         {/* Enable the constraints again .*/
+            for (String constraint : this.disabled.get(table))
+            {
+               enableConstraint(connection, table , constraint);
+            }
+         }
+      }
+   }
+
+   @Override
+   public void relax(Connection connection) throws SQLException
+   {
+      if (null != this.tableNames && !this.tableNames.isEmpty())
+      {
+         PreparedStatement enabledConstraintsByTable = connection.prepareStatement("SELECT constraint_name FROM all_constraints WHERE (table_name=?) AND (status='ENABLED') AND (constraint_type='R')");
+         
+         for(String table : this.tableNames)
+         {
+            
+            /* Initially get all the enabled constraints for this table. */
+            // TODO: build a batch of statements to execute
+            List<String> constraintNames = getEnabledConstraints(table, enabledConstraintsByTable);
+            
+            for (String constraint : constraintNames)
+            {
+               disableConstraint(connection, table, constraint);
+            }
+         
+         }
+         DbUtils.closeQuietly(enabledConstraintsByTable);
+      }
+   }
+
+   private List<String> getEnabledConstraints(String tableName, PreparedStatement enabledConstraints)
+   {
+      List<String> returnValue = new ArrayList<String>();
+      ResultSet resultSet = null;
+      try
+      {
+         enabledConstraints.setString(1, tableName);
+         resultSet = enabledConstraints.executeQuery();
+         while (resultSet.next())
+         {
+            returnValue.add(resultSet.getString("CONSTRAINT_NAME"));
+         }
+      }
+      catch (SQLException sqle)
+      {
+         logger.error(sqle);
+      }
+      finally
+      {
+         DbUtils.closeQuietly(resultSet);
+      }
+      
+      return  returnValue;
+   }
+   
+   private void disableConstraint(Connection conn, String table, String constraintName)
+   {
+      ResultSet resultSet = null;
+      PreparedStatement statement = null;
+      try
+      {
+         statement = conn.prepareStatement("ALTER TABLE "+table+" MODIFY CONSTRAINT "+constraintName+" DISABLE NOVALIDATE"); 
+         resultSet = statement.executeQuery();
+      }
+      catch (SQLException sqle)
+      {
+         logger.error(sqle);
+      }
+      finally
+      {
+         DbUtils.closeQuietly(resultSet);
+         DbUtils.closeQuietly(statement);
+      }
+   }
+   private void enableConstraint(Connection conn, String table, String constraint)
+   {
+      ResultSet resultSet = null;
+      PreparedStatement statement = null;
+      try
+      {
+         statement = conn.prepareStatement("ALTER TABLE "+table+" MODIFY CONSTRAINT "+constraint+" ENABLE VALIDATE"); 
+         resultSet = statement.executeQuery();
+      }
+      catch (SQLException sqle)
+      {
+         logger.error(sqle);
+      }
+      finally
+      {
+         DbUtils.closeQuietly(resultSet);
+         DbUtils.closeQuietly(statement);
+      }
+   }
+}



More information about the jboss-svn-commits mailing list