LISTSERV mailing list manager LISTSERV 16.5

Help for HPS-SVN Archives


HPS-SVN Archives

HPS-SVN Archives


HPS-SVN@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

HPS-SVN Home

HPS-SVN Home

HPS-SVN  November 2014

HPS-SVN November 2014

Subject:

r1581 - /java/trunk/conditions/src/main/java/org/hps/conditions/database/DatabaseConditionsManager.java

From:

[log in to unmask]

Reply-To:

Notification of commits to the hps svn repository <[log in to unmask]>

Date:

Tue, 25 Nov 2014 09:50:50 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (145 lines)

Author: [log in to unmask]
Date: Tue Nov 25 01:50:46 2014
New Revision: 1581

Log:
Add a method for inserting a collection in a transaction using a SQL prepared statement.

Modified:
    java/trunk/conditions/src/main/java/org/hps/conditions/database/DatabaseConditionsManager.java

Modified: java/trunk/conditions/src/main/java/org/hps/conditions/database/DatabaseConditionsManager.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/database/DatabaseConditionsManager.java	(original)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/database/DatabaseConditionsManager.java	Tue Nov 25 01:50:46 2014
@@ -11,17 +11,21 @@
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.sql.Connection;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map.Entry;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.hps.conditions.api.ConditionsObject;
 import org.hps.conditions.api.ConditionsObjectCollection;
+import org.hps.conditions.api.ConditionsObjectException;
 import org.hps.conditions.api.ConditionsRecord;
 import org.hps.conditions.api.ConditionsRecord.ConditionsRecordCollection;
 import org.hps.conditions.api.ConditionsSeries;
@@ -58,19 +62,21 @@
 @SuppressWarnings("rawtypes")
 public class DatabaseConditionsManager extends ConditionsManagerImplementation {
 
-    static String connectionProperty = "org.hps.conditions.connection.file";
+    protected static final String CONNECTION_PROPERTY = "org.hps.conditions.connection.file";
+    protected static final String DEFAULT_CONFIG = "/org/hps/conditions/config/conditions_dev.xml";
+    protected static final String TEST_RUN_CONFIG = "/org/hps/conditions/config/conditions_database_testrun_2012.xml";
+    protected static final int TEST_RUN_MAX_RUN = 1365;
+
+    protected static Logger logger = LogUtil.create(DatabaseConditionsManager.class);
+    
     protected String detectorName;
     protected List<TableMetaData> tableMetaData;
     protected List<ConditionsConverter> converters;
     protected File connectionPropertiesFile;
-    protected static Logger logger = LogUtil.create(DatabaseConditionsManager.class);
     protected ConnectionParameters connectionParameters = new DefaultConnectionParameters();
     protected Connection connection;
     protected boolean isConnected = false;
     protected ConditionsSeriesConverter conditionsSeriesConverter = new ConditionsSeriesConverter(this);
-    static final String DEFAULT_CONFIG = "/org/hps/conditions/config/conditions_dev.xml";
-    static final String TEST_RUN_CONFIG = "/org/hps/conditions/config/conditions_database_testrun_2012.xml";
-    protected static final int TEST_RUN_MAX_RUN = 1365;
     protected boolean isInitialized = false;
     protected String resourceConfig = null;
     protected File fileConfig = null;
@@ -81,6 +87,7 @@
     protected TestRunSvtDetectorSetup testRunSvtloader = new TestRunSvtDetectorSetup();
     protected SvtDetectorSetup svtLoader = new SvtDetectorSetup();
     protected String tag = null;
+    //protected boolean dryRun = false;
     
     /**
      * Default connection parameters which will use the SLAC database by default,
@@ -588,7 +595,57 @@
     public void setTag(String tag) {
         this.tag = tag;
     }
-       
+
+    public <ObjectType extends ConditionsObject> void insertCollection(ConditionsObjectCollection<ObjectType> collection) throws SQLException {
+        if (collection == null) {
+            throw new IllegalArgumentException("The collection is null.");
+        }
+        if (collection.getObjects().size() == 0) {
+            throw new IllegalArgumentException("The collection is empty.");
+        }
+        if (collection.getTableMetaData() == null) {
+            throw new RuntimeException("The collection does not have table meta data.");
+        }
+        TableMetaData tableMetaData = collection.getTableMetaData();
+        if (collection.getCollectionId() == -1) {
+            try {
+                collection.setCollectionId(this.getNextCollectionID(tableMetaData.getTableName()));
+            } catch (ConditionsObjectException e) {
+                throw new RuntimeException(e);
+            }
+        }
+        logger.fine("inserting collection with ID " + collection.getCollectionId() 
+                + " and key " + collection.getTableMetaData().getKey() + " into table " + tableMetaData.getTableName());
+
+        try {
+            connection.setAutoCommit(false);
+            logger.finest("starting insert transaction");
+            String sql = QueryBuilder.buildPreparedInsert(collection.get(0));
+            PreparedStatement preparedStatement = 
+                connection.prepareStatement(sql);
+            logger.finest("using prepared statement: " + sql);
+            logger.finest("preparing updates");
+            for (ConditionsObject object : collection.getObjects()) {
+                preparedStatement.setObject(1, object.getCollectionId());
+                int parameterIndex = 2;
+                for (Entry<String,Object> entry : object.getFieldValues().entrySet()) {
+                    preparedStatement.setObject(parameterIndex, entry.getValue());
+                    ++parameterIndex;
+                }
+                preparedStatement.executeUpdate();
+            }
+            logger.finest("done preparing updates");
+            connection.commit();
+            logger.finest("committed transaction");
+        } catch (Exception e) {
+            logger.warning("rolling back transaction");
+            connection.rollback();
+            logger.warning("transaction was rolled back");
+        } finally {
+            connection.setAutoCommit(true);
+        }
+    }
+           
     private void setupEcal() {
         logger.config("setting up ECAL conditions on detector");
         EcalConditions conditions = getCachedConditions(EcalConditions.class, ECAL_CONDITIONS).getCachedData();
@@ -691,11 +748,11 @@
      * {@link #setConnectionProperties(File)} or {@link #setConnectionResource(String)}.
      */
     private void setupConnectionFromSystemProperty() {
-        String systemPropertiesConnectionPath = (String) System.getProperties().get(connectionProperty);
+        String systemPropertiesConnectionPath = (String) System.getProperties().get(CONNECTION_PROPERTY);
         if (systemPropertiesConnectionPath != null) {
             File f = new File(systemPropertiesConnectionPath);
             if (!f.exists()) {
-                throw new RuntimeException("Connection properties file from " + connectionProperty + " does not exist.");
+                throw new RuntimeException("Connection properties file from " + CONNECTION_PROPERTY + " does not exist.");
             }
             this.setConnectionProperties(f);
         }

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2017
August 2017
July 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use