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/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();