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/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}