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