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  January 2015

HPS-SVN January 2015

Subject:

r1949 - /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:

Fri, 16 Jan 2015 23:59:36 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (137 lines)

Author: [log in to unmask]
Date: Fri Jan 16 15:59:31 2015
New Revision: 1949

Log:
Add some methods for managing connections better in query methods.

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	Fri Jan 16 15:59:31 2015
@@ -159,11 +159,21 @@
 
         return (DatabaseConditionsManager) manager;
     }
+    
+    public boolean checkConnection() {
+        if (!isConnected()) {
+            openConnection();
+            return true;
+        } else {
+            return false;
+        }
+    }
 
     /**
      * Open the database connection.
      */
-    public Connection openConnection() {
+    public boolean openConnection() {
+        boolean openedConnection = false;
         if (!isConnected) {
             // Do the connection parameters need to be figured out automatically?
             if (connectionParameters == null) {
@@ -183,10 +193,13 @@
 
             // Create the connection using the parameters.
             connection = connectionParameters.createConnection();
-            isConnected = true;
+            isConnected = true;                        
+            openedConnection = true;
         } 
         logger.info("connection opened");
-        return connection;
+        
+        // Flag to indicate whether an existing connection was used or not.
+        return openedConnection;
     }
 
     /**
@@ -205,6 +218,17 @@
         connection = null;
         isConnected = false;
         logger.info("connection closed");
+    }
+    
+    /**
+     * Close the database connection but only if there was a connection opened
+     * based on the flag.  Otherwise, it should be left open.
+     * @param connectionOpened Whether or not to close the connection.
+     */
+    public void closeConnection(boolean connectionOpened) {
+        if (connectionOpened) {
+            closeConnection();
+        }
     }
 
     /**
@@ -458,6 +482,7 @@
      * @return The next collection ID.
      */
     public int getNextCollectionID(String tableName) {
+        boolean openedConnection = openConnection();
         TableMetaData tableData = tableRegistry.findByTableName(tableName);
         if (tableData == null)
             throw new IllegalArgumentException("There is no meta data for table " + tableName);
@@ -471,6 +496,7 @@
             logger.warning(e.getMessage());
         }
         logger.fine("new collection ID " + collectionId + " created for table " + tableName);
+        closeConnection(openedConnection);
         return collectionId;
     }
 
@@ -526,7 +552,7 @@
      * @return The keys of the rows affected.
      */
     public List<Integer> updateQuery(String query) {
-        openConnection();
+        boolean openedConnection = openConnection();
         logger.fine(query);
         List<Integer> keys = new ArrayList<Integer>();
         Statement statement = null;
@@ -543,7 +569,7 @@
             throw new RuntimeException("Error in SQL query: " + query, x);
         } 
         DatabaseUtilities.cleanup(resultSet);
-        closeConnection();
+        closeConnection(openedConnection);        
         return keys;
     }
 
@@ -683,7 +709,7 @@
         TableMetaData tableMetaData = collection.getTableMetaData();
         if (tableMetaData == null) {            
             tableMetaData = tableRegistry.findByCollectionType(collection.getClass()); 
-            logger.fine("using default table meta data with table " + tableMetaData.getTableName() + " for collection of type " + collection.getClass().getCanonicalName());
+            logger.fine("using default table meta data with table " + tableMetaData.getTableName() + " for collection of type " + collection.getClass().getCanonicalName());            
         }
         if (collection.getCollectionId() == -1) {
             try {
@@ -693,7 +719,7 @@
             }
         }
         logger.info("inserting collection with ID " + collection.getCollectionId() 
-                + " and key " + collection.getTableMetaData().getKey() + " into table " + tableMetaData.getTableName());
+                + " and key " + tableMetaData.getKey() + " into table " + tableMetaData.getTableName());
 
         boolean openedConnection = false;
         if (!isConnected()) {
@@ -737,10 +763,8 @@
             preparedStatement.close();
         } catch (Exception e) {
         }
-        
-        if (openedConnection) {
-            closeConnection();
-        }
+               
+        closeConnection(openedConnection);        
     }
     
     /**

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