Changeset 316

Show
Ignore:
Timestamp:
02/25/10 20:57:30 (5 months ago)
Author:
martijn
Message:

refactor LazyPst? to also handle resultset and connection

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

Legend:

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

    r308 r316  
    1717package org.bridgedb.rdb; 
    1818 
     19import java.io.PrintWriter; 
    1920import java.sql.Connection; 
    2021import java.sql.PreparedStatement; 
     
    6162        } 
    6263         
    63         /** 
    64          * helper class that handles lazy initialization of all prepared statements used by SimpleGdb 
    65          * Non-static, because it needs the con database connection field. 
    66          */ 
    67         protected final class LazyPst 
     64        private boolean keepConnection = true; 
     65 
     66        /** 
     67         * helper class that handles the life cycle of a connection, query and resultset. 
     68         * <p> 
     69         * The sql for a query is passed in at construction time. 
     70         * Before each query, call init(). This will lead to lazy initialization of the 
     71         * connection and preparedstatement objects, if necessary. Set the query parameters 
     72         * using setString(int, String). Get the resultSet using  
     73         * Do not close the resultset! This will be closed for you when you call cleanup(). 
     74         * Always call cleanup() in a finally block. 
     75         * <p> 
     76         * The advantages of using QueryLifeCycle are: 
     77         * <ul> 
     78         * <li>guarantee to close preparedstatement, resultset and connection if necessary. 
     79         * <li>in case of connection pooling, preparedstatement and connection are kept together as long 
     80         *   as possible. 
     81         * <li>lazy initialization of prepared statement 
     82         * <li>always uses preparedstatement, so safe from SQL injection. 
     83         * </ul>  
     84         * <p> 
     85         * This class is not static because it needs SimpleGdb.getConnection(). 
     86         */ 
     87        final class QueryLifeCycle 
    6888        { 
    6989                /** 
     
    7292                 * @param aSql SQL query 
    7393                 */ 
    74                 public LazyPst(String aSql) 
     94                public QueryLifeCycle(String aSql) 
    7595                { 
    7696                        sql = aSql; 
    7797                } 
    7898                 
     99                private Connection con = null; 
     100                private ResultSet rs = null; 
    79101                private PreparedStatement pst = null; 
    80102                private final String sql; 
     103                private boolean inited = false; 
     104 
     105                public static final int QUERY_TIMEOUT = 20; //seconds 
     106                public static final int NO_LIMIT = 0; 
     107                public static final int NO_TIMEOUT = 0; 
     108 
     109                public void init(int limit) throws SQLException 
     110                { 
     111                        init(); 
     112                        pst.setQueryTimeout(QUERY_TIMEOUT);                      
     113                        if(limit > NO_LIMIT)  
     114                        { 
     115                                pst.setMaxRows(limit); 
     116                        } 
     117                } 
    81118                 
    82119                /** 
    83                  * Get a PreparedStatement using lazy initialization. 
     120                 * Initialize connection and PreparedStatement lazily. 
    84121                 * <p> 
    85                  * Assumes SimpleGdbImpl2.con is already valid 
    86                  * @return a prepared statement for the given query. 
    87122                 * @throws SQLException when a PreparedStatement could not be created 
    88123                 */ 
    89                 public PreparedStatement getPreparedStatement() throws SQLException 
    90                 { 
    91                         if (pst == null) 
     124                public void init() throws SQLException 
     125                { 
     126                        if (inited) throw new IllegalStateException("Must call cleanup() between two init() calls"); 
     127                        try 
    92128                        { 
    93                                 pst = con.prepareStatement(sql); 
     129                                if (con == null) con = getConnection(); 
     130                                if (pst == null) 
     131                                { 
     132                                        pst = con.prepareStatement(sql); 
     133                                } 
    94134                        } 
    95                         return pst; 
    96                 } 
    97         } 
    98  
     135                        finally { inited = true; } 
     136                } 
     137                 
     138                public void setString (int index, String val) throws SQLException 
     139                { 
     140                        if (!inited) throw new IllegalStateException("Must call init() before setString()"); 
     141                        pst.setString(index, val); 
     142                } 
     143                 
     144                public ResultSet executeQuery() throws SQLException 
     145                { 
     146                        if (!inited) throw new IllegalStateException("Must call init() before executeQuery()"); 
     147                        rs = pst.executeQuery(); 
     148                        return rs; 
     149                } 
     150 
     151                /**  
     152                 * Clean up resultset. If keepConnection is false, preparedstatement 
     153                 * and connection are cached. If keepConnection is true, they are closed as well. 
     154                 * The later is useful when using connection pooling. 
     155                 * <p> 
     156                 * Always call this in a finally block!  
     157                 * */ 
     158                public void cleanup() 
     159                { 
     160                        if (!inited) throw new IllegalStateException("Must call init() before cleanup()"); 
     161                        inited = false; 
     162                        if (rs != null) try { rs.close(); } catch (SQLException ignore) {} 
     163                        if (keepConnection) return; 
     164                        if (pst != null) try { pst.close(); } catch (SQLException ignore) {} 
     165                        if (con != null) try { con.close(); } catch (SQLException ignore) {} 
     166                } 
     167        } 
     168         
     169        protected Connection getConnection() throws SQLException 
     170        { 
     171                return con; 
     172        } 
     173         
    99174        /** 
    100175         * The {@link Connection} to the Gene Database. 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl2.java

    r308 r316  
    4444        private static final int GDB_COMPAT_VERSION = 2; //Preferred schema version 
    4545         
    46         private final SimpleGdb.LazyPst pstDatasources = new SimpleGdb.LazyPst( 
     46        private final SimpleGdb.QueryLifeCycle qDatasources = new SimpleGdb.QueryLifeCycle( 
    4747                        "SELECT codeRight FROM link GROUP BY codeRight" 
    4848                ); 
    49         private final SimpleGdb.LazyPst pstInfo = new SimpleGdb.LazyPst( 
     49        private final SimpleGdb.QueryLifeCycle qInfo = new SimpleGdb.QueryLifeCycle( 
    5050                        "SELECT * FROM info" 
    5151                ); 
    52         private final SimpleGdb.LazyPst pstXrefExists = new SimpleGdb.LazyPst( 
     52        private final SimpleGdb.QueryLifeCycle qXrefExists = new SimpleGdb.QueryLifeCycle( 
    5353                        "SELECT id FROM " + "datanode" + " WHERE " + 
    5454                        "id = ? AND code = ?" 
    5555                ); 
    56         private final SimpleGdb.LazyPst pstBackpage = new SimpleGdb.LazyPst( 
     56        private final SimpleGdb.QueryLifeCycle qBackpage = new SimpleGdb.QueryLifeCycle( 
    5757                        "SELECT backpageText FROM datanode " + 
    5858                        " WHERE id = ? AND code = ?" 
    5959                ); 
    60         private final SimpleGdb.LazyPst pstAttribute = new SimpleGdb.LazyPst( 
     60        private final SimpleGdb.QueryLifeCycle qAttribute = new SimpleGdb.QueryLifeCycle( 
    6161                        "SELECT attrvalue FROM attribute " + 
    6262                        " WHERE id = ? AND code = ? AND attrname = ?" 
    6363                ); 
    64         private final SimpleGdb.LazyPst pstAllAttributes = new SimpleGdb.LazyPst( 
     64        private final SimpleGdb.QueryLifeCycle qAllAttributes = new SimpleGdb.QueryLifeCycle( 
    6565                        "SELECT attrname, attrvalue FROM attribute " + 
    6666                        " WHERE id = ? AND code = ?" 
    6767                ); 
    68         private final SimpleGdb.LazyPst pstAttributesSet = new SimpleGdb.LazyPst( 
     68        private final SimpleGdb.QueryLifeCycle qAttributesSet = new SimpleGdb.QueryLifeCycle( 
    6969                        "SELECT attrname FROM attribute GROUP BY attrname" 
    7070                ); 
    71         private final SimpleGdb.LazyPst pstCrossRefs = new SimpleGdb.LazyPst ( 
     71        private final SimpleGdb.QueryLifeCycle qCrossRefs = new SimpleGdb.QueryLifeCycle ( 
    7272                        "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 
    7373                        "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 
    7474                        "WHERE src.idRight = ? AND src.codeRight = ?" 
    7575                ); 
    76         private final SimpleGdb.LazyPst pstCrossRefsWithCode = new SimpleGdb.LazyPst ( 
     76        private final SimpleGdb.QueryLifeCycle qCrossRefsWithCode = new SimpleGdb.QueryLifeCycle ( 
    7777                        "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 
    7878                        "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 
    7979                        "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?" 
    8080                ); 
    81         private final SimpleGdb.LazyPst pstRefsByAttribute = new SimpleGdb.LazyPst ( 
     81        private final SimpleGdb.QueryLifeCycle qRefsByAttribute = new SimpleGdb.QueryLifeCycle ( 
    8282                        "SELECT datanode.id, datanode.code FROM datanode " + 
    8383                        " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " + 
    8484                        "WHERE attrName = ? AND attrValue = ?" 
    8585                ); 
    86         private final SimpleGdb.LazyPst pstFreeSearch = new SimpleGdb.LazyPst ( 
     86        private final SimpleGdb.QueryLifeCycle qFreeSearch = new SimpleGdb.QueryLifeCycle ( 
    8787                        "SELECT id, code FROM datanode WHERE " + 
    8888                        "LOWER(ID) LIKE ?" 
    8989                ); 
    90         private final SimpleGdb.LazyPst pstAttributeSearch = new SimpleGdb.LazyPst ( 
     90        private final SimpleGdb.QueryLifeCycle qAttributeSearch = new SimpleGdb.QueryLifeCycle ( 
    9191                        "SELECT id, code, attrvalue FROM attribute WHERE " + 
    9292                        "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?" 
    9393                ); 
    94         private final SimpleGdb.LazyPst pstIdSearchWithAttributes = new SimpleGdb.LazyPst ( 
     94        private final SimpleGdb.QueryLifeCycle qIdSearchWithAttributes = new SimpleGdb.QueryLifeCycle ( 
    9595                        "SELECT id, code, attrvalue FROM attribute WHERE " + 
    9696                        "attrname = 'Symbol' AND LOWER(ID) LIKE ?" 
    9797                ); 
    98          
     98 
    9999        /** {@inheritDoc} */ 
    100100        public boolean xrefExists(Xref xref) throws IDMapperException  
    101101        { 
     102                final QueryLifeCycle pst = qXrefExists; 
    102103                try  
    103104                { 
    104                         PreparedStatement pst = pstXrefExists.getPreparedStatement(); 
     105                        pst.init(); 
    105106                        pst.setString(1, xref.getId()); 
    106107                        pst.setString(2, xref.getDataSource().getSystemCode()); 
     
    116117                        throw new IDMapperException (e); 
    117118                } 
     119                finally {pst.cleanup(); } 
    118120                return false; 
    119121        } 
     
    126128        private Map<String, String> getInfo() throws IDMapperException 
    127129        { 
     130                final QueryLifeCycle pst = qInfo; 
    128131                Map<String, String> result = new HashMap<String, String>(); 
    129132                try 
    130133                { 
    131                         PreparedStatement pst = pstInfo.getPreparedStatement(); 
     134                        pst.init(); 
    132135                        ResultSet rs = pst.executeQuery(); 
    133136                         
     
    162165        private String getBpInfo(Xref ref) throws IDMapperException  
    163166        { 
     167                final QueryLifeCycle pst = qBackpage; 
    164168                try { 
    165                         PreparedStatement pst = pstBackpage.getPreparedStatement(); 
     169                        pst.init(); 
    166170                        pst.setString (1, ref.getId()); 
    167171                        pst.setString (2, ref.getDataSource().getSystemCode()); 
     
    174178                        return result; 
    175179                } catch (SQLException e) { throw new IDMapperException (e); } //Gene not found 
     180                finally {pst.cleanup(); } 
    176181        } 
    177182 
     
    179184        public Set<Xref> mapID (Xref idc, DataSource... resultDs) throws IDMapperException 
    180185        { 
     186                final QueryLifeCycle pst = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode; 
    181187                Set<Xref> refs = new HashSet<Xref>(); 
    182188                 
     
    184190                try 
    185191                { 
    186                         PreparedStatement pst; 
    187                         if (resultDs.length != 1) 
    188                         { 
    189                                 pst = pstCrossRefs.getPreparedStatement(); 
    190                         } 
    191                         else 
    192                         { 
    193                                 pst = pstCrossRefsWithCode.getPreparedStatement(); 
    194                                 pst.setString(3, resultDs[0].getSystemCode()); 
    195                         } 
    196                          
     192                        pst.init(); 
    197193                        pst.setString(1, idc.getId()); 
    198194                        pst.setString(2, idc.getDataSource().getSystemCode()); 
     195                        if (resultDs.length == 1) pst.setString(3, resultDs[0].getSystemCode());                         
    199196                         
    200197                        Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); 
     
    214211                        throw new IDMapperException (e); 
    215212                } 
     213                finally {pst.cleanup(); } 
    216214                 
    217215                return refs; 
     
    222220//              Logger.log.trace("Fetching cross references by attribute: " + attrName + " = " + attrValue); 
    223221                List<Xref> refs = new ArrayList<Xref>(); 
    224  
     222                final QueryLifeCycle pst = qRefsByAttribute; 
    225223                try { 
    226                         PreparedStatement pst = pstRefsByAttribute.getPreparedStatement(); 
     224                        pst.init(); 
    227225                        pst.setString(1, attrName); 
    228226                        pst.setString(2, attrValue); 
     
    235233                        throw new IDMapperException (e); 
    236234                } 
     235                finally {pst.cleanup(); } 
    237236//              Logger.log.trace("End fetching cross references by attribute"); 
    238237                return refs; 
     
    373372        {                
    374373                Set<Xref> result = new HashSet<Xref>(); 
     374                final QueryLifeCycle pst = qFreeSearch; 
    375375                try { 
    376                         PreparedStatement ps1 = pstFreeSearch.getPreparedStatement(); 
    377                         ps1.setQueryTimeout(QUERY_TIMEOUT); 
    378                         if(limit > NO_LIMIT)  
    379                         { 
    380                                 ps1.setMaxRows(limit); 
    381                         } 
    382  
    383                         ps1.setString(1, "%" + text.toLowerCase() + "%"); 
    384                         ResultSet r = ps1.executeQuery(); 
     376                        pst.init(limit); 
     377                        pst.setString(1, "%" + text.toLowerCase() + "%"); 
     378                        ResultSet r = pst.executeQuery(); 
    385379                        while(r.next()) { 
    386380                                String id = r.getString(1); 
     
    394388                        throw new IDMapperException(e); 
    395389                } 
     390                finally {pst.cleanup(); } 
    396391                return result; 
    397392        } 
     
    530525        { 
    531526                Set<DataSource> result = new HashSet<DataSource>(); 
     527                final QueryLifeCycle pst = qDatasources; 
    532528        try 
    533529        { 
    534                 PreparedStatement pst = pstDatasources.getPreparedStatement(); 
     530                pst.init(); 
    535531                ResultSet rs = pst.executeQuery(); 
    536532                while (rs.next()) 
     
    544540                throw new IDMapperException(ignore); 
    545541        } 
     542                finally {pst.cleanup(); } 
    546543        return result; 
    547544        } 
     
    585582        { 
    586583                Set<String> result = new HashSet<String>(); 
     584                final QueryLifeCycle pst = qAttribute; 
    587585                 
    588586                if (ATTRIBUTES_FROM_BACKPAGE.containsKey(attrname)) 
     
    601599                 
    602600                try { 
    603                         PreparedStatement pst = pstAttribute.getPreparedStatement(); 
     601                        pst.init(); 
    604602                        pst.setString (1, ref.getId()); 
    605603                        pst.setString (2, ref.getDataSource().getSystemCode()); 
     
    612610                        return result; 
    613611                } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref + ", Attribute: " + attrname, e); } // Database unavailable 
     612                finally {pst.cleanup(); } 
    614613        } 
    615614 
     
    619618        { 
    620619                Map<String, Set<String>> result = new HashMap<String, Set<String>>(); 
     620                final QueryLifeCycle pst = qAllAttributes; 
    621621                                 
    622622                String bpInfo = getBpInfo(ref); 
     
    637637                 
    638638                try { 
    639                         PreparedStatement pst = pstAllAttributes.getPreparedStatement(); 
     639                        pst.init(); 
    640640                        pst.setString (1, ref.getId()); 
    641641                        pst.setString (2, ref.getDataSource().getSystemCode()); 
     
    658658                        return result; 
    659659                } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable 
     660                finally {pst.cleanup(); } 
    660661        } 
    661662 
     
    680681        { 
    681682                Map<Xref, String> result = new HashMap<Xref, String>(); 
     683                final QueryLifeCycle pst = (MATCH_ID.equals (attrType)) ?  
     684                                qIdSearchWithAttributes : qAttributeSearch; 
    682685                try { 
    683                         PreparedStatement pst = (MATCH_ID.equals (attrType)) ?  
    684                                         pstIdSearchWithAttributes.getPreparedStatement() : pstAttributeSearch.getPreparedStatement(); 
    685                         pst.setQueryTimeout(QUERY_TIMEOUT); 
    686                         if(limit > NO_LIMIT) pst.setMaxRows(limit); 
     686                        pst.init(limit); 
    687687                        pst.setString(1, "%" + query.toLowerCase() + "%"); 
    688688                        ResultSet r = pst.executeQuery(); 
     
    698698                        throw new IDMapperException (e); 
    699699                } 
     700                finally {pst.cleanup(); } 
    700701                return result;           
    701702        } 
     
    705706        { 
    706707                Set<String> result = new HashSet<String>(); 
     708                final QueryLifeCycle pst = qAttributesSet; 
    707709        try 
    708710        { 
    709                 PreparedStatement pst = pstAttributesSet.getPreparedStatement(); 
     711                pst.init(); 
    710712                ResultSet rs = pst.executeQuery(); 
    711713                while (rs.next()) 
     
    718720                throw new IDMapperException(ignore); 
    719721        } 
     722                finally {pst.cleanup(); } 
    720723        return result; 
    721724        } 
  • trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl3.java

    r308 r316  
    4242        private static final int GDB_COMPAT_VERSION = 3; //Preferred schema version 
    4343         
    44         private final SimpleGdb.LazyPst pstDatasources = new SimpleGdb.LazyPst( 
     44        private final SimpleGdb.QueryLifeCycle qDatasources = new SimpleGdb.QueryLifeCycle( 
    4545                        "SELECT codeRight FROM link GROUP BY codeRight" 
    4646                ); 
    47         private final SimpleGdb.LazyPst pstInfo = new SimpleGdb.LazyPst( 
     47        private final SimpleGdb.QueryLifeCycle qInfo = new SimpleGdb.QueryLifeCycle( 
    4848                        "SELECT * FROM info" 
    4949                ); 
    50         private final SimpleGdb.LazyPst pstXrefExists = new SimpleGdb.LazyPst( 
     50        private final SimpleGdb.QueryLifeCycle qXrefExists = new SimpleGdb.QueryLifeCycle( 
    5151                        "SELECT id FROM " + "datanode" + " WHERE " + 
    5252                        "id = ? AND code = ?" 
    5353                );       
    54         private final SimpleGdb.LazyPst pstAttribute = new SimpleGdb.LazyPst( 
     54        private final SimpleGdb.QueryLifeCycle qAttribute = new SimpleGdb.QueryLifeCycle( 
    5555                        "SELECT attrvalue FROM attribute " + 
    5656                        " WHERE id = ? AND code = ? AND attrname = ?" 
    5757                ); 
    58         private final SimpleGdb.LazyPst pstAllAttributes = new SimpleGdb.LazyPst( 
     58        private final SimpleGdb.QueryLifeCycle qAllAttributes = new SimpleGdb.QueryLifeCycle( 
    5959                        "SELECT attrname, attrvalue FROM attribute " + 
    6060                        " WHERE id = ? AND code = ?" 
    6161                ); 
    62         private final SimpleGdb.LazyPst pstAttributesSet = new SimpleGdb.LazyPst( 
     62        private final SimpleGdb.QueryLifeCycle qAttributesSet = new SimpleGdb.QueryLifeCycle( 
    6363                        "SELECT attrname FROM attribute GROUP BY attrname" 
    6464                ); 
    65         private final SimpleGdb.LazyPst pstCrossRefs = new SimpleGdb.LazyPst ( 
     65        private final SimpleGdb.QueryLifeCycle qCrossRefs = new SimpleGdb.QueryLifeCycle ( 
    6666                        "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 
    6767                        "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 
    6868                        "WHERE src.idRight = ? AND src.codeRight = ?" 
    6969                ); 
    70         private final SimpleGdb.LazyPst pstCrossRefsWithCode = new SimpleGdb.LazyPst ( 
     70        private final SimpleGdb.QueryLifeCycle qCrossRefsWithCode = new SimpleGdb.QueryLifeCycle ( 
    7171                        "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 
    7272                        "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 
    7373                        "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?" 
    7474                ); 
    75         private final SimpleGdb.LazyPst pstRefsByAttribute = new SimpleGdb.LazyPst ( 
     75        private final SimpleGdb.QueryLifeCycle qRefsByAttribute = new SimpleGdb.QueryLifeCycle ( 
    7676                        "SELECT datanode.id, datanode.code FROM datanode " + 
    7777                        " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " + 
    7878                        "WHERE attrName = ? AND attrValue = ?" 
    7979                ); 
    80         private final SimpleGdb.LazyPst pstFreeSearch = new SimpleGdb.LazyPst ( 
     80        private final SimpleGdb.QueryLifeCycle qFreeSearch = new SimpleGdb.QueryLifeCycle ( 
    8181                        "SELECT id, code FROM datanode WHERE " + 
    8282                        "LOWER(ID) LIKE ?" 
    8383                ); 
    84         private final SimpleGdb.LazyPst pstAttributeSearch = new SimpleGdb.LazyPst ( 
     84        private final SimpleGdb.QueryLifeCycle qAttributeSearch = new SimpleGdb.QueryLifeCycle ( 
    8585                        "SELECT id, code, attrvalue FROM attribute WHERE " + 
    8686                        "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?" 
    8787                ); 
    88         private final SimpleGdb.LazyPst pstIdSearchWithAttributes = new SimpleGdb.LazyPst ( 
     88        private final SimpleGdb.QueryLifeCycle qIdSearchWithAttributes = new SimpleGdb.QueryLifeCycle ( 
    8989                        "SELECT id, code, attrvalue FROM attribute WHERE " + 
    9090                        "attrname = 'Symbol' AND LOWER(ID) LIKE ?" 
    9191                ); 
    92          
     92 
    9393        /** {@inheritDoc} */ 
    9494        public boolean xrefExists(Xref xref) throws IDMapperException  
    9595        { 
     96                final QueryLifeCycle pst = qXrefExists; 
    9697                try  
    9798                { 
    98                         PreparedStatement pst = pstXrefExists.getPreparedStatement(); 
     99                        pst.init(); 
    99100                        pst.setString(1, xref.getId()); 
    100101                        pst.setString(2, xref.getDataSource().getSystemCode()); 
     
    110111                        throw new IDMapperException (e); 
    111112                } 
     113                finally {pst.cleanup(); } 
    112114                return false; 
    113115        } 
     
    117119        { 
    118120                Set<Xref> refs = new HashSet<Xref>(); 
    119                  
     121                final QueryLifeCycle pst = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode;       
    120122                try 
    121123                { 
    122                         PreparedStatement pst; 
    123                         if (resultDs.length != 1) 
    124                         { 
    125                                 pst = pstCrossRefs.getPreparedStatement(); 
    126                         } 
    127                         else 
    128                         { 
    129                                 pst = pstCrossRefsWithCode.getPreparedStatement(); 
    130                                 pst.setString(3, resultDs[0].getSystemCode()); 
    131                         } 
    132                          
     124                        pst.init();                      
    133125                        pst.setString(1, idc.getId()); 
    134126                        pst.setString(2, idc.getDataSource().getSystemCode()); 
     127                        if (resultDs.length == 1) 
     128                        { 
     129                                pst.setString(3, resultDs[0].getSystemCode()); 
     130                        } 
    135131 
    136132                        Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); 
     
    150146                        throw new IDMapperException (e); 
    151147                } 
     148                finally {pst.cleanup(); } 
    152149                 
    153150                return refs; 
     
    159156                List<Xref> refs = new ArrayList<Xref>(); 
    160157 
     158                final QueryLifeCycle pst = qRefsByAttribute; 
    161159                try { 
    162                         PreparedStatement pst = pstRefsByAttribute.getPreparedStatement(); 
     160                        pst.init(); 
    163161                        pst.setString(1, attrName); 
    164162                        pst.setString(2, attrValue); 
     
    171169                        throw new IDMapperException (e); 
    172170                } 
     171                finally {pst.cleanup(); } 
    173172//              Logger.log.trace("End fetching cross references by attribute"); 
    174173                return refs; 
     
    308307        {                
    309308                Set<Xref> result = new HashSet<Xref>(); 
     309                final QueryLifeCycle pst = qFreeSearch; 
    310310                try { 
    311                         PreparedStatement ps1 = pstFreeSearch.getPreparedStatement(); 
    312                         ps1.setQueryTimeout(QUERY_TIMEOUT); 
    313                         if(limit > NO_LIMIT)  
    314                         { 
    315                                 ps1.setMaxRows(limit); 
    316                         } 
    317  
    318                         ps1.setString(1, "%" + text.toLowerCase() + "%"); 
    319                         ResultSet r = ps1.executeQuery(); 
     311                        pst.init(limit); 
     312                        pst.setString(1, "%" + text.toLowerCase() + "%"); 
     313                        ResultSet r = pst.executeQuery(); 
    320314                        while(r.next()) { 
    321315                                String id = r.getString(1); 
     
    329323                        throw new IDMapperException(e); 
    330324                } 
     325                finally {pst.cleanup(); } 
    331326                return result; 
    332327        } 
     
    465460        { 
    466461                Map<String, String> result = new HashMap<String, String>(); 
     462                final QueryLifeCycle pst = qInfo; 
    467463                try 
    468464                { 
    469                         PreparedStatement pst = pstInfo.getPreparedStatement(); 
     465                        pst.init(); 
    470466                        ResultSet rs = pst.executeQuery(); 
    471467                         
     
    496492        { 
    497493                Set<DataSource> result = new HashSet<DataSource>(); 
     494                final QueryLifeCycle pst = qDatasources; 
    498495        try 
    499496        { 
    500                 PreparedStatement pst = pstDatasources.getPreparedStatement(); 
     497                        pst.init(); 
    501498                ResultSet rs = pst.executeQuery(); 
    502499                while (rs.next()) 
     
    539536        { 
    540537                Set<String> result = new HashSet<String>(); 
     538                final QueryLifeCycle pst = qAttribute; 
    541539                try { 
    542                         PreparedStatement pst = pstAttribute.getPreparedStatement(); 
     540                        pst.init(); 
    543541                        pst.setString (1, ref.getId()); 
    544542                        pst.setString (2, ref.getDataSource().getSystemCode()); 
     
    551549                        return result; 
    552550                } catch (SQLException e) { throw new IDMapperException (e); } // Database unavailable 
     551                finally {pst.cleanup(); } 
    553552        } 
    554553 
     
    558557        { 
    559558                Map<String, Set<String>> result = new HashMap<String, Set<String>>();                            
     559                final QueryLifeCycle pst = qAllAttributes; 
    560560                try { 
    561                         PreparedStatement pst = pstAllAttributes.getPreparedStatement(); 
     561                        pst.init(); 
    562562                        pst.setString (1, ref.getId()); 
    563563                        pst.setString (2, ref.getDataSource().getSystemCode()); 
     
    580580                        return result; 
    581581                } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable 
     582                finally {pst.cleanup(); } 
    582583        } 
    583584 
     
    602603        { 
    603604                Map<Xref, String> result = new HashMap<Xref, String>(); 
     605                final QueryLifeCycle pst = (MATCH_ID.equals (attrType)) ?  
     606                                qIdSearchWithAttributes : qAttributeSearch; 
    604607                try { 
    605                         PreparedStatement pst = (MATCH_ID.equals (attrType)) ?  
    606                                         pstIdSearchWithAttributes.getPreparedStatement() : pstAttributeSearch.getPreparedStatement(); 
    607                         pst.setQueryTimeout(QUERY_TIMEOUT); 
    608                         if(limit > NO_LIMIT) pst.setMaxRows(limit); 
     608                        pst.init(limit); 
    609609                        pst.setString(1, "%" + query.toLowerCase() + "%"); 
    610610                        ResultSet r = pst.executeQuery(); 
     
    620620                        throw new IDMapperException (e); 
    621621                } 
     622                finally {pst.cleanup(); } 
    622623                return result;           
    623624        } 
     
    627628        { 
    628629                Set<String> result = new HashSet<String>(); 
     630                final QueryLifeCycle pst = qAttributesSet; 
    629631        try 
    630632        { 
    631                 PreparedStatement pst = pstAttributesSet.getPreparedStatement(); 
     633                        pst.init(); 
    632634                ResultSet rs = pst.executeQuery(); 
    633635                while (rs.next()) 
     
    640642                throw new IDMapperException(ignore); 
    641643        } 
     644                finally {pst.cleanup(); } 
    642645        return result; 
    643646        }