Changeset 318

Show
Ignore:
Timestamp:
02/25/10 20:57:43 (2 years ago)
Author:
martijn
Message:

Made SimpleGdb? thread-safe. Instantiate with con-
nection string instead of connection object, to allow
future implementation of thread pooling

Location:
trunk/org.bridgedb.rdb/src/org/bridgedb/rdb
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/IDMapperRdb.java

    r317 r318  
    5656                public IDMapper connect(String location) throws IDMapperException  
    5757                { 
    58                         try 
    59                         { 
    60                                 String url = "jdbc:derby:jar:(" + location + ")database"; 
    61                                 Connection con = DriverManager.getConnection(url); 
    62                                 return SimpleGdbFactory.createInstance(location, con); 
    63                         } 
    64                         catch (SQLException ex) 
    65                         { 
    66                                 throw new IDMapperException(ex); 
    67                         } 
     58                        String url = "jdbc:derby:jar:(" + location + ")database"; 
     59                        return SimpleGdbFactory.createInstance(location, url); 
    6860                } 
    6961        } 
     
    7769                public IDMapper connect(String location) throws IDMapperException  
    7870                { 
    79                         try 
    80                         { 
    81                                 String url = "jdbc:" + location; 
    82                                 Connection con = DriverManager.getConnection(url); 
    83                                 return SimpleGdbFactory.createInstance(location, con); 
    84                         } 
    85                         catch (SQLException ex) 
    86                         { 
    87                                 throw new IDMapperException(ex); 
    88                         } 
     71                        String url = "jdbc:" + location; 
     72                        return SimpleGdbFactory.createInstance(location, url); 
    8973                } 
    9074        } 
     
    11599                                 
    116100                                String url = "jdbc:derby://" + host + ":" + port + "/" + args.get("BASE"); 
    117                                 Connection con = DriverManager.getConnection(url); 
    118                                 return SimpleGdbFactory.createInstance(location, con); 
    119                         } 
    120                         catch (SQLException f) 
    121                         { 
    122                                 throw new IDMapperException (f); 
     101                                return SimpleGdbFactory.createInstance(location, url); 
    123102                        } 
    124103                        catch (IOException e) 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdb.java

    r317 r318  
    1818 
    1919import java.sql.Connection; 
     20import java.sql.DriverManager; 
    2021import java.sql.PreparedStatement; 
    2122import java.sql.ResultSet; 
     
    5253public abstract class SimpleGdb extends IDMapperRdb 
    5354{ 
     55        private final String connectionString; 
    5456        /** 
    5557         * Create IDMapper based on an existing SQL connection. 
    5658         * @param con Existing SQL Connection. 
    5759         */ 
    58         SimpleGdb(Connection con) 
    59         { 
    60                 this.con = con; 
    61         } 
    62          
    63         private boolean keepConnection = true; 
    64  
     60        SimpleGdb(String dbName, String connectionString) 
     61        { 
     62                this.connectionString = connectionString; 
     63                this.dbName = dbName; 
     64        } 
     65 
     66        private boolean singleConnection = true; 
     67        private boolean neverCloseConnection = true; 
     68         
    6569        /** 
    6670         * helper class that handles the life cycle of a connection, query and resultset. 
     
    160164                        inited = false; 
    161165                        if (rs != null) try { rs.close(); } catch (SQLException ignore) {} 
    162                         if (keepConnection) return; 
     166                        if (neverCloseConnection) return; 
    163167                        if (pst != null) try { pst.close(); } catch (SQLException ignore) {} 
     168                        pst = null; 
    164169                        if (con != null) try { con.close(); } catch (SQLException ignore) {} 
    165                 } 
    166         } 
    167          
    168         protected Connection getConnection() throws SQLException 
    169         { 
     170                        con = null; 
     171                } 
     172        } 
     173 
     174        private Connection con = null; 
     175         
     176        synchronized public Connection getConnection() throws SQLException 
     177        { 
     178                // if singleConnection is true, each call to getConnection() will return the same object. 
     179                // if singleConnection is false, each call to getConneciton() will lead to a new connection object being created. 
     180                if (!singleConnection || con == null) 
     181                { 
     182                        con = DriverManager.getConnection(connectionString);  
     183                        con.setReadOnly(true); 
     184                } 
    170185                return con; 
    171186        } 
     
    174189         * The {@link Connection} to the Gene Database. 
    175190         */ 
    176         protected Connection con = null; 
     191         
     192        //private Connection con = null; 
    177193 
    178194        /** {@inheritDoc} */ 
    179         final public boolean isConnected() { return con != null; } 
    180  
    181         protected String dbName; 
     195        final public boolean isConnected() {  
     196                //return con != null;  
     197                return true; 
     198        } 
     199 
     200        protected final String dbName; 
    182201         
    183202        /** {@inheritDoc} */ 
     
    187206        final public void close() throws IDMapperException  
    188207        { 
    189                 if (con == null) throw new IDMapperException("Database connection already closed"); 
    190                 try 
    191                 { 
    192                         con.close(); 
    193                 } 
    194                 catch (SQLException ex) 
    195                 { 
    196                         throw new IDMapperException (ex); 
    197                 } 
    198                 con = null; 
     208//              try 
     209//              { 
     210//                      con.close(); 
     211//              } 
     212//              catch (SQLException ex) 
     213//              { 
     214//                      throw new IDMapperException (ex); 
     215//              } 
     216//              con = null; 
    199217        } 
    200218         
     
    212230                try 
    213231                { 
    214                         ResultSet r = con.createStatement().executeQuery("SELECT COUNT(*) FROM " + "datanode"); 
     232                        ResultSet r = getConnection().createStatement().executeQuery("SELECT COUNT(*) FROM " + "datanode"); 
    215233                        r.next(); 
    216234                        result = r.getInt (1); 
     
    234252                try 
    235253                { 
    236                         ResultSet r = con.createStatement().executeQuery( 
     254                        ResultSet r = getConnection().createStatement().executeQuery( 
    237255                                        "SELECT COUNT(*) FROM datanode WHERE code = '" + ds.getSystemCode() + "'"); 
    238256                        r.next(); 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbFactory.java

    r317 r318  
    1818 
    1919import java.sql.Connection; 
     20import java.sql.DriverManager; 
    2021import java.sql.ResultSet; 
    2122import java.sql.SQLException; 
     23import java.sql.Statement; 
    2224 
    2325import org.bridgedb.IDMapperException; 
     
    3840         * <p> 
    3941         * Use this instead of constructor to create an instance of SimpleGdb that matches the schema version. 
    40          * @param dbName The file containing the Gene Database.  
    41          * @param con An SQL Connection to the database 
    42          * @param props PROP_RECREATE if you want to create a new database, overwriting any existing ones. Otherwise, PROP_NONE. 
     42         * @param connectionString a JDBC Connection string  
    4343         * @return a new Gdb 
    4444         * @throws IDMapperException on failure 
    4545        */ 
    46         public static SimpleGdb createInstance(String dbName, Connection con) throws IDMapperException 
     46        public static SimpleGdb createInstance(String dbName, String connectionString) throws IDMapperException 
    4747        { 
    48                 if(dbName == null) throw new NullPointerException();     
     48                if(connectionString == null) throw new NullPointerException();   
    4949 
    5050                int version = 0; 
     51                Connection con = null; 
     52                ResultSet r = null; 
     53                Statement stmt = null; 
    5154                try  
    5255                { 
    53                         ResultSet r = con.createStatement().executeQuery("SELECT schemaversion FROM info"); 
     56                        con = DriverManager.getConnection(connectionString); 
     57                        stmt = con.createStatement(); 
     58                        r = stmt.executeQuery("SELECT schemaversion FROM info"); 
    5459                        if(r.next()) version = r.getInt(1); 
    5560                }  
     
    5762                { 
    5863                        //Ignore, older db's don't even have schema version 
    59                 }                        
     64                } 
     65                finally 
     66                { 
     67                        if (r != null) try { r.close(); } catch (SQLException ignore) {} 
     68                        if (stmt != null) try { stmt.close(); } catch (SQLException ignore) {} 
     69                        if (con != null) try { con.close(); } catch (SQLException ignore) {} 
     70                } 
    6071                 
    6172                switch (version) 
    6273                { 
    6374                case 2: 
    64                         return new SimpleGdbImpl2(dbName, con); 
     75                        return new SimpleGdbImpl2(dbName, connectionString); 
    6576                case 3: 
    66                         return new SimpleGdbImpl3(dbName, con); 
     77                        return new SimpleGdbImpl3(dbName, connectionString); 
    6778                //NB add future schema versions here 
    6879                default: 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl2.java

    r317 r318  
    5151        { 
    5252                final QueryLifeCycle pst = qBackpage; 
    53                 try { 
    54                         pst.init(); 
    55                         pst.setString (1, ref.getId()); 
    56                         pst.setString (2, ref.getDataSource().getSystemCode()); 
    57                         ResultSet r = pst.executeQuery(); 
    58                         String result = null; 
    59                         if (r.next()) 
    60                         { 
    61                                 result = r.getString(1); 
    62                         } 
    63                         return result; 
    64                 } catch (SQLException e) { throw new IDMapperException (e); } //Gene not found 
    65                 finally {pst.cleanup(); } 
     53                synchronized (pst) 
     54                { 
     55                        try { 
     56                                pst.init(); 
     57                                pst.setString (1, ref.getId()); 
     58                                pst.setString (2, ref.getDataSource().getSystemCode()); 
     59                                ResultSet r = pst.executeQuery(); 
     60                                String result = null; 
     61                                if (r.next()) 
     62                                { 
     63                                        result = r.getString(1); 
     64                                } 
     65                                return result; 
     66                        } catch (SQLException e) { throw new IDMapperException (e); } //Gene not found 
     67                        finally {pst.cleanup(); } 
     68                } 
    6669        } 
    6770 
     
    7578         * @throws IDMapperException when the database could not be created or connected to 
    7679         */ 
    77         public SimpleGdbImpl2(String dbName, Connection con) throws IDMapperException 
    78         { 
    79                 super (con); 
    80                  
    81                 if(dbName == null) throw new NullPointerException(); 
    82                 this.dbName = dbName; 
    83                  
    84                 try 
    85                 { 
    86                         con.setReadOnly(true); 
    87                 } 
    88                 catch (SQLException e) 
    89                 { 
    90                         throw new IDMapperException (e); 
    91                 } 
     80        public SimpleGdbImpl2(String dbName, String connectionString) throws IDMapperException 
     81        { 
     82                super (dbName, connectionString); 
     83                 
     84                if(dbName == null) throw new NullPointerException();             
    9285                checkSchemaVersion(); 
    9386        } 
     
    10295                try  
    10396                { 
    104                         ResultSet r = con.createStatement().executeQuery("SELECT schemaversion FROM info"); 
     97                        ResultSet r = getConnection().createStatement().executeQuery("SELECT schemaversion FROM info"); 
    10598                        if(r.next()) version = r.getInt(1); 
    10699                }  
     
    148141                } 
    149142                 
    150                 try { 
    151                         pst.init(); 
    152                         pst.setString (1, ref.getId()); 
    153                         pst.setString (2, ref.getDataSource().getSystemCode()); 
    154                         pst.setString (3, attrname); 
    155                         ResultSet r = pst.executeQuery(); 
    156                         if (r.next()) 
    157                         { 
    158                                 result.add (r.getString(1)); 
    159                         } 
    160                         return result; 
    161                 } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref + ", Attribute: " + attrname, e); } // Database unavailable 
    162                 finally {pst.cleanup(); } 
     143                synchronized (pst) 
     144                { 
     145                        try { 
     146                                pst.init(); 
     147                                pst.setString (1, ref.getId()); 
     148                                pst.setString (2, ref.getDataSource().getSystemCode()); 
     149                                pst.setString (3, attrname); 
     150                                ResultSet r = pst.executeQuery(); 
     151                                if (r.next()) 
     152                                { 
     153                                        result.add (r.getString(1)); 
     154                                } 
     155                                return result; 
     156                        } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref + ", Attribute: " + attrname, e); } // Database unavailable 
     157                        finally {pst.cleanup(); } 
     158                } 
    163159        } 
    164160 
     
    186182                } 
    187183                 
    188                 try { 
    189                         pst.init(); 
    190                         pst.setString (1, ref.getId()); 
    191                         pst.setString (2, ref.getDataSource().getSystemCode()); 
    192                         ResultSet r = pst.executeQuery(); 
    193                         if (r.next()) 
    194                         { 
    195                                 String key = r.getString(1); 
    196                                 String value = r.getString(2); 
    197                                 if (result.containsKey (key)) 
    198                                 { 
    199                                         result.get(key).add (value); 
    200                                 } 
    201                                 else 
    202                                 { 
    203                                         Set<String> valueSet = new HashSet<String>(); 
    204                                         valueSet.add (value); 
    205                                         result.put (key, valueSet); 
    206                                 } 
    207                         } 
    208                         return result; 
    209                 } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable 
    210                 finally {pst.cleanup(); } 
     184                synchronized (pst) 
     185                { 
     186                        try { 
     187                                pst.init(); 
     188                                pst.setString (1, ref.getId()); 
     189                                pst.setString (2, ref.getDataSource().getSystemCode()); 
     190                                ResultSet r = pst.executeQuery(); 
     191                                if (r.next()) 
     192                                { 
     193                                        String key = r.getString(1); 
     194                                        String value = r.getString(2); 
     195                                        if (result.containsKey (key)) 
     196                                        { 
     197                                                result.get(key).add (value); 
     198                                        } 
     199                                        else 
     200                                        { 
     201                                                Set<String> valueSet = new HashSet<String>(); 
     202                                                valueSet.add (value); 
     203                                                result.put (key, valueSet); 
     204                                        } 
     205                                } 
     206                                return result; 
     207                        } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable 
     208                        finally {pst.cleanup(); } 
     209                } 
    211210        } 
    212211} 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl3.java

    r317 r318  
    4242         * @throws IDMapperException when the database could not be created or connected to 
    4343         */ 
    44         public SimpleGdbImpl3(String dbName, Connection con) throws IDMapperException 
     44        public SimpleGdbImpl3(String dbName, String connectionString) throws IDMapperException 
    4545        { 
    46                 super(con); 
    47                 if(dbName == null) throw new NullPointerException(); 
    48  
    49                 this.dbName = dbName; 
    50  
    51                 try 
    52                 { 
    53                         con.setReadOnly(true); 
    54                 } 
    55                 catch (SQLException e) 
    56                 { 
    57                         throw new IDMapperException (e); 
    58                 } 
     46                super(dbName, connectionString); 
    5947                checkSchemaVersion(); 
    6048        } 
     
    6957                try  
    7058                { 
    71                         ResultSet r = con.createStatement().executeQuery("SELECT schemaversion FROM info"); 
     59                        ResultSet r = getConnection().createStatement().executeQuery("SELECT schemaversion FROM info"); 
    7260                        if(r.next()) version = r.getInt(1); 
    7361                }  
     
    8876                Set<String> result = new HashSet<String>(); 
    8977                final QueryLifeCycle pst = qAttribute; 
    90                 try { 
    91                         pst.init(); 
    92                         pst.setString (1, ref.getId()); 
    93                         pst.setString (2, ref.getDataSource().getSystemCode()); 
    94                         pst.setString (3, attrname); 
    95                         ResultSet r = pst.executeQuery(); 
    96                         if (r.next()) 
    97                         { 
    98                                 result.add (r.getString(1)); 
    99                         } 
    100                         return result; 
    101                 } catch (SQLException e) { throw new IDMapperException (e); } // Database unavailable 
    102                 finally {pst.cleanup(); } 
     78                synchronized (pst) 
     79                { 
     80                        try { 
     81                                pst.init(); 
     82                                pst.setString (1, ref.getId()); 
     83                                pst.setString (2, ref.getDataSource().getSystemCode()); 
     84                                pst.setString (3, attrname); 
     85                                ResultSet r = pst.executeQuery(); 
     86                                if (r.next()) 
     87                                { 
     88                                        result.add (r.getString(1)); 
     89                                } 
     90                                return result; 
     91                        } catch (SQLException e) { throw new IDMapperException (e); } // Database unavailable 
     92                        finally {pst.cleanup(); } 
     93                } 
    10394        } 
    10495 
     
    109100                Map<String, Set<String>> result = new HashMap<String, Set<String>>();                            
    110101                final QueryLifeCycle pst = qAllAttributes; 
    111                 try { 
    112                         pst.init(); 
    113                         pst.setString (1, ref.getId()); 
    114                         pst.setString (2, ref.getDataSource().getSystemCode()); 
    115                         ResultSet r = pst.executeQuery(); 
    116                         if (r.next()) 
    117                         { 
    118                                 String key = r.getString(1); 
    119                                 String value = r.getString(2); 
    120                                 if (result.containsKey (key)) 
     102                synchronized (pst) 
     103                { 
     104                        try { 
     105                                pst.init(); 
     106                                pst.setString (1, ref.getId()); 
     107                                pst.setString (2, ref.getDataSource().getSystemCode()); 
     108                                ResultSet r = pst.executeQuery(); 
     109                                if (r.next()) 
    121110                                { 
    122                                         result.get(key).add (value); 
     111                                        String key = r.getString(1); 
     112                                        String value = r.getString(2); 
     113                                        if (result.containsKey (key)) 
     114                                        { 
     115                                                result.get(key).add (value); 
     116                                        } 
     117                                        else 
     118                                        { 
     119                                                Set<String> valueSet = new HashSet<String>(); 
     120                                                valueSet.add (value); 
     121                                                result.put (key, valueSet); 
     122                                        } 
    123123                                } 
    124                                 else 
    125                                 { 
    126                                         Set<String> valueSet = new HashSet<String>(); 
    127                                         valueSet.add (value); 
    128                                         result.put (key, valueSet); 
    129                                 } 
    130                         } 
    131                         return result; 
    132                 } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable 
    133                 finally {pst.cleanup(); } 
     124                                return result; 
     125                        } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable 
     126                        finally {pst.cleanup(); } 
     127                } 
    134128        } 
    135129} 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImplCommon.java

    r317 r318  
    11package org.bridgedb.rdb; 
    22 
    3 import java.sql.Connection; 
    43import java.sql.ResultSet; 
    54import java.sql.ResultSetMetaData; 
     
    2423public abstract class SimpleGdbImplCommon extends SimpleGdb 
    2524{ 
    26         SimpleGdbImplCommon(Connection con) throws IDMapperException 
    27         { 
    28                 super(con); 
     25        SimpleGdbImplCommon(String dbName, String connectionString) throws IDMapperException 
     26        { 
     27                super(dbName, connectionString); 
    2928                caps = new SimpleGdbCapabilities(); 
    3029        } 
     
    8382        { 
    8483                final QueryLifeCycle pst = qXrefExists; 
    85                 try  
    86                 { 
    87                         pst.init(); 
    88                         pst.setString(1, xref.getId()); 
    89                         pst.setString(2, xref.getDataSource().getSystemCode()); 
    90                         ResultSet r = pst.executeQuery(); 
    91  
    92                         while(r.next())  
    93                         { 
    94                                 return true; 
    95                         } 
    96                 }  
    97                 catch (SQLException e)  
    98                 { 
    99                         throw new IDMapperException (e); 
    100                 } 
    101                 finally {pst.cleanup(); } 
    102                 return false; 
     84                synchronized (pst) { 
     85                        try  
     86                        { 
     87                                pst.init(); 
     88                                pst.setString(1, xref.getId()); 
     89                                pst.setString(2, xref.getDataSource().getSystemCode()); 
     90                                ResultSet r = pst.executeQuery(); 
     91         
     92                                while(r.next())  
     93                                { 
     94                                        return true; 
     95                                } 
     96                        }  
     97                        catch (SQLException e)  
     98                        { 
     99                                throw new IDMapperException (e); 
     100                        } 
     101                        finally {pst.cleanup(); } 
     102                        return false; 
     103                } 
    103104        } 
    104105 
     
    110111        Map<String, String> getInfo() throws IDMapperException 
    111112        { 
     113                Map<String, String> result = new HashMap<String, String>(); 
    112114                final QueryLifeCycle pst = qInfo; 
    113                 Map<String, String> result = new HashMap<String, String>(); 
    114                 try 
    115                 { 
    116                         pst.init(); 
    117                         ResultSet rs = pst.executeQuery(); 
     115                synchronized (pst) { 
     116                        try 
     117                        { 
     118                                pst.init(); 
     119                                ResultSet rs = pst.executeQuery(); 
     120                                 
     121                                if (rs.next()) 
     122                                { 
     123                                        ResultSetMetaData rsmd = rs.getMetaData(); 
     124                                        for (int i = 1; i <= rsmd.getColumnCount(); ++i) 
     125                                        { 
     126                                                String key = rsmd.getColumnName(i); 
     127                                                String val = rs.getString(i); 
     128                                                result.put (key, val); 
     129                                        } 
     130                                } 
     131                        } 
     132                        catch (SQLException ex) 
     133                        { 
     134                                throw new IDMapperException (ex); 
     135                        } 
    118136                         
    119                         if (rs.next()) 
    120                         { 
    121                                 ResultSetMetaData rsmd = rs.getMetaData(); 
    122                                 for (int i = 1; i <= rsmd.getColumnCount(); ++i) 
    123                                 { 
    124                                         String key = rsmd.getColumnName(i); 
    125                                         String val = rs.getString(i); 
    126                                         result.put (key, val); 
    127                                 } 
    128                         } 
    129                 } 
    130                 catch (SQLException ex) 
    131                 { 
    132                         throw new IDMapperException (ex); 
    133                 } 
    134                  
    135                 return result; 
     137                        return result; 
     138                } 
    136139        } 
    137140 
     
    144147                 
    145148                if (idc.getDataSource() == null) return refs; 
    146                 try 
    147                 { 
    148                         pst.init(); 
    149                         pst.setString(1, idc.getId()); 
    150                         pst.setString(2, idc.getDataSource().getSystemCode()); 
    151                         if (resultDs.length == 1) pst.setString(3, resultDs[0].getSystemCode());                         
    152                          
    153                         Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); 
    154  
    155                         ResultSet rs = pst.executeQuery(); 
    156                         while (rs.next()) 
    157                         { 
    158                                 DataSource ds = DataSource.getBySystemCode(rs.getString(2)); 
    159                                 if (resultDs.length == 0 || dsFilter.contains(ds)) 
     149                synchronized (pst) { 
     150                        try 
     151                        { 
     152                                pst.init(); 
     153                                pst.setString(1, idc.getId()); 
     154                                pst.setString(2, idc.getDataSource().getSystemCode()); 
     155                                if (resultDs.length == 1) pst.setString(3, resultDs[0].getSystemCode());                         
     156                                 
     157                                Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); 
     158         
     159                                ResultSet rs = pst.executeQuery(); 
     160                                while (rs.next()) 
    160161                                { 
    161                                         refs.add (new Xref (rs.getString(1), ds)); 
    162                                 } 
    163                         } 
    164                 } 
    165                 catch (SQLException e) 
    166                 { 
    167                         throw new IDMapperException (e); 
    168                 } 
    169                 finally {pst.cleanup(); } 
     162                                        DataSource ds = DataSource.getBySystemCode(rs.getString(2)); 
     163                                        if (resultDs.length == 0 || dsFilter.contains(ds)) 
     164                                        { 
     165                                                refs.add (new Xref (rs.getString(1), ds)); 
     166                                        } 
     167                                } 
     168                        } 
     169                        catch (SQLException e) 
     170                        { 
     171                                throw new IDMapperException (e); 
     172                        } 
     173                        finally {pst.cleanup(); } 
    170174                 
    171                 return refs; 
     175                        return refs; 
     176                } 
    172177        } 
    173178 
     
    178183 
    179184                final QueryLifeCycle pst = qRefsByAttribute; 
    180                 try { 
    181                         pst.init(); 
    182                         pst.setString(1, attrName); 
    183                         pst.setString(2, attrValue); 
    184                         ResultSet r = pst.executeQuery(); 
    185                         while(r.next()) { 
    186                                 Xref ref = new Xref(r.getString(1), DataSource.getBySystemCode(r.getString(2))); 
    187                                 refs.add(ref); 
    188                         } 
    189                 } catch(SQLException e) { 
    190                         throw new IDMapperException (e); 
    191                 } 
    192                 finally {pst.cleanup(); } 
    193 //              Logger.log.trace("End fetching cross references by attribute"); 
    194                 return refs; 
     185                synchronized (pst) {  
     186                        try { 
     187                                pst.init(); 
     188                                pst.setString(1, attrName); 
     189                                pst.setString(2, attrValue); 
     190                                ResultSet r = pst.executeQuery(); 
     191                                while(r.next()) { 
     192                                        Xref ref = new Xref(r.getString(1), DataSource.getBySystemCode(r.getString(2))); 
     193                                        refs.add(ref); 
     194                                } 
     195                        } catch(SQLException e) { 
     196                                throw new IDMapperException (e); 
     197                        } 
     198                        finally {pst.cleanup(); } 
     199        //              Logger.log.trace("End fetching cross references by attribute"); 
     200                        return refs; 
     201                } 
    195202        } 
    196203 
     
    200207                Set<Xref> result = new HashSet<Xref>(); 
    201208                final QueryLifeCycle pst = qFreeSearch; 
    202                 try { 
    203                         pst.init(limit); 
    204                         pst.setString(1, "%" + text.toLowerCase() + "%"); 
    205                         ResultSet r = pst.executeQuery(); 
    206                         while(r.next()) { 
    207                                 String id = r.getString(1); 
    208                                 DataSource ds = DataSource.getBySystemCode(r.getString(2)); 
    209                                 Xref ref = new Xref (id, ds); 
    210                                 result.add (ref); 
    211                         }                        
    212                 }  
    213                 catch (SQLException e)  
    214                 { 
    215                         throw new IDMapperException(e); 
    216                 } 
    217                 finally {pst.cleanup(); } 
    218                 return result; 
     209                synchronized (pst) {  
     210                        try { 
     211                                pst.init(limit); 
     212                                pst.setString(1, "%" + text.toLowerCase() + "%"); 
     213                                ResultSet r = pst.executeQuery(); 
     214                                while(r.next()) { 
     215                                        String id = r.getString(1); 
     216                                        DataSource ds = DataSource.getBySystemCode(r.getString(2)); 
     217                                        Xref ref = new Xref (id, ds); 
     218                                        result.add (ref); 
     219                                }                        
     220                        }  
     221                        catch (SQLException e)  
     222                        { 
     223                                throw new IDMapperException(e); 
     224                        } 
     225                        finally {pst.cleanup(); } 
     226                        return result; 
     227                } 
    219228        } 
    220229 
     
    227236                Set<DataSource> result = new HashSet<DataSource>(); 
    228237                final QueryLifeCycle pst = qDatasources; 
    229         try 
    230         { 
    231                 pst.init(); 
    232                 ResultSet rs = pst.executeQuery(); 
    233                 while (rs.next()) 
    234                 { 
    235                         DataSource ds = DataSource.getBySystemCode(rs.getString(1));  
    236                         result.add (ds); 
    237                 } 
    238         } 
    239         catch (SQLException ignore) 
    240         { 
    241                 throw new IDMapperException(ignore); 
    242         } 
    243                 finally {pst.cleanup(); } 
    244         return result; 
     238                synchronized (pst) {  
     239                        try 
     240                { 
     241                        pst.init(); 
     242                        ResultSet rs = pst.executeQuery(); 
     243                        while (rs.next()) 
     244                        { 
     245                                DataSource ds = DataSource.getBySystemCode(rs.getString(1));  
     246                                result.add (ds); 
     247                        } 
     248                } 
     249                catch (SQLException ignore) 
     250                { 
     251                        throw new IDMapperException(ignore); 
     252                } 
     253                        finally {pst.cleanup(); } 
     254                return result; 
     255                } 
    245256        } 
    246257 
     
    288299                final QueryLifeCycle pst = (MATCH_ID.equals (attrType)) ?  
    289300                                qIdSearchWithAttributes : qAttributeSearch; 
    290                 try { 
    291                         pst.init(limit); 
    292                         pst.setString(1, "%" + query.toLowerCase() + "%"); 
    293                         ResultSet r = pst.executeQuery(); 
    294  
    295                         while(r.next())  
    296                         { 
    297                                 String id = r.getString("id"); 
    298                                 String code = r.getString("code"); 
    299                                 String symbol = r.getString("attrValue"); 
    300                                 result.put(new Xref (id, DataSource.getBySystemCode(code)), symbol); 
    301                         } 
    302                 } catch (SQLException e) { 
    303                         throw new IDMapperException (e); 
    304                 } 
    305                 finally {pst.cleanup(); } 
    306                 return result;           
     301                synchronized (pst) {  
     302                        try { 
     303                                pst.init(limit); 
     304                                pst.setString(1, "%" + query.toLowerCase() + "%"); 
     305                                ResultSet r = pst.executeQuery(); 
     306         
     307                                while(r.next())  
     308                                { 
     309                                        String id = r.getString("id"); 
     310                                        String code = r.getString("code"); 
     311                                        String symbol = r.getString("attrValue"); 
     312                                        result.put(new Xref (id, DataSource.getBySystemCode(code)), symbol); 
     313                                } 
     314                        } catch (SQLException e) { 
     315                                throw new IDMapperException (e); 
     316                        } 
     317                        finally {pst.cleanup(); } 
     318                        return result; 
     319                } 
    307320        } 
    308321 
     
    312325                Set<String> result = new HashSet<String>(); 
    313326                final QueryLifeCycle pst = qAttributesSet; 
    314         try 
    315         { 
    316                 pst.init(); 
    317                 ResultSet rs = pst.executeQuery(); 
    318                 while (rs.next()) 
    319                 { 
    320                         result.add (rs.getString(1)); 
    321                 } 
    322         } 
    323         catch (SQLException ignore) 
    324         { 
    325                 throw new IDMapperException(ignore); 
    326         } 
    327                 finally {pst.cleanup(); } 
    328         return result; 
     327                synchronized (pst) {  
     328                try 
     329                { 
     330                        pst.init(); 
     331                        ResultSet rs = pst.executeQuery(); 
     332                        while (rs.next()) 
     333                        { 
     334                                result.add (rs.getString(1)); 
     335                        } 
     336                } 
     337                catch (SQLException ignore) 
     338                { 
     339                        throw new IDMapperException(ignore); 
     340                } 
     341                        finally {pst.cleanup(); } 
     342                return result; 
     343                } 
    329344        } 
    330345