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

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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