Changeset 316
- Timestamp:
- 02/25/10 20:57:30 (5 months ago)
- Location:
- trunk/org.bridgedb.rdb/src/org/bridgedb/rdb
- Files:
-
- 3 modified
-
SimpleGdb.java (modified) (3 diffs)
-
SimpleGdbImpl2.java (modified) (24 diffs)
-
SimpleGdbImpl3.java (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdb.java
r308 r316 17 17 package org.bridgedb.rdb; 18 18 19 import java.io.PrintWriter; 19 20 import java.sql.Connection; 20 21 import java.sql.PreparedStatement; … … 61 62 } 62 63 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 68 88 { 69 89 /** … … 72 92 * @param aSql SQL query 73 93 */ 74 public LazyPst(String aSql)94 public QueryLifeCycle(String aSql) 75 95 { 76 96 sql = aSql; 77 97 } 78 98 99 private Connection con = null; 100 private ResultSet rs = null; 79 101 private PreparedStatement pst = null; 80 102 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 } 81 118 82 119 /** 83 * Get a PreparedStatement using lazy initialization.120 * Initialize connection and PreparedStatement lazily. 84 121 * <p> 85 * Assumes SimpleGdbImpl2.con is already valid86 * @return a prepared statement for the given query.87 122 * @throws SQLException when a PreparedStatement could not be created 88 123 */ 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 92 128 { 93 pst = con.prepareStatement(sql); 129 if (con == null) con = getConnection(); 130 if (pst == null) 131 { 132 pst = con.prepareStatement(sql); 133 } 94 134 } 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 99 174 /** 100 175 * The {@link Connection} to the Gene Database. -
trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl2.java
r308 r316 44 44 private static final int GDB_COMPAT_VERSION = 2; //Preferred schema version 45 45 46 private final SimpleGdb. LazyPst pstDatasources = new SimpleGdb.LazyPst(46 private final SimpleGdb.QueryLifeCycle qDatasources = new SimpleGdb.QueryLifeCycle( 47 47 "SELECT codeRight FROM link GROUP BY codeRight" 48 48 ); 49 private final SimpleGdb. LazyPst pstInfo = new SimpleGdb.LazyPst(49 private final SimpleGdb.QueryLifeCycle qInfo = new SimpleGdb.QueryLifeCycle( 50 50 "SELECT * FROM info" 51 51 ); 52 private final SimpleGdb. LazyPst pstXrefExists = new SimpleGdb.LazyPst(52 private final SimpleGdb.QueryLifeCycle qXrefExists = new SimpleGdb.QueryLifeCycle( 53 53 "SELECT id FROM " + "datanode" + " WHERE " + 54 54 "id = ? AND code = ?" 55 55 ); 56 private final SimpleGdb. LazyPst pstBackpage = new SimpleGdb.LazyPst(56 private final SimpleGdb.QueryLifeCycle qBackpage = new SimpleGdb.QueryLifeCycle( 57 57 "SELECT backpageText FROM datanode " + 58 58 " WHERE id = ? AND code = ?" 59 59 ); 60 private final SimpleGdb. LazyPst pstAttribute = new SimpleGdb.LazyPst(60 private final SimpleGdb.QueryLifeCycle qAttribute = new SimpleGdb.QueryLifeCycle( 61 61 "SELECT attrvalue FROM attribute " + 62 62 " WHERE id = ? AND code = ? AND attrname = ?" 63 63 ); 64 private final SimpleGdb. LazyPst pstAllAttributes = new SimpleGdb.LazyPst(64 private final SimpleGdb.QueryLifeCycle qAllAttributes = new SimpleGdb.QueryLifeCycle( 65 65 "SELECT attrname, attrvalue FROM attribute " + 66 66 " WHERE id = ? AND code = ?" 67 67 ); 68 private final SimpleGdb. LazyPst pstAttributesSet = new SimpleGdb.LazyPst(68 private final SimpleGdb.QueryLifeCycle qAttributesSet = new SimpleGdb.QueryLifeCycle( 69 69 "SELECT attrname FROM attribute GROUP BY attrname" 70 70 ); 71 private final SimpleGdb. LazyPst pstCrossRefs = new SimpleGdb.LazyPst(71 private final SimpleGdb.QueryLifeCycle qCrossRefs = new SimpleGdb.QueryLifeCycle ( 72 72 "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 73 73 "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 74 74 "WHERE src.idRight = ? AND src.codeRight = ?" 75 75 ); 76 private final SimpleGdb. LazyPst pstCrossRefsWithCode = new SimpleGdb.LazyPst(76 private final SimpleGdb.QueryLifeCycle qCrossRefsWithCode = new SimpleGdb.QueryLifeCycle ( 77 77 "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 78 78 "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 79 79 "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?" 80 80 ); 81 private final SimpleGdb. LazyPst pstRefsByAttribute = new SimpleGdb.LazyPst(81 private final SimpleGdb.QueryLifeCycle qRefsByAttribute = new SimpleGdb.QueryLifeCycle ( 82 82 "SELECT datanode.id, datanode.code FROM datanode " + 83 83 " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " + 84 84 "WHERE attrName = ? AND attrValue = ?" 85 85 ); 86 private final SimpleGdb. LazyPst pstFreeSearch = new SimpleGdb.LazyPst(86 private final SimpleGdb.QueryLifeCycle qFreeSearch = new SimpleGdb.QueryLifeCycle ( 87 87 "SELECT id, code FROM datanode WHERE " + 88 88 "LOWER(ID) LIKE ?" 89 89 ); 90 private final SimpleGdb. LazyPst pstAttributeSearch = new SimpleGdb.LazyPst(90 private final SimpleGdb.QueryLifeCycle qAttributeSearch = new SimpleGdb.QueryLifeCycle ( 91 91 "SELECT id, code, attrvalue FROM attribute WHERE " + 92 92 "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?" 93 93 ); 94 private final SimpleGdb. LazyPst pstIdSearchWithAttributes = new SimpleGdb.LazyPst(94 private final SimpleGdb.QueryLifeCycle qIdSearchWithAttributes = new SimpleGdb.QueryLifeCycle ( 95 95 "SELECT id, code, attrvalue FROM attribute WHERE " + 96 96 "attrname = 'Symbol' AND LOWER(ID) LIKE ?" 97 97 ); 98 98 99 99 /** {@inheritDoc} */ 100 100 public boolean xrefExists(Xref xref) throws IDMapperException 101 101 { 102 final QueryLifeCycle pst = qXrefExists; 102 103 try 103 104 { 104 PreparedStatement pst = pstXrefExists.getPreparedStatement();105 pst.init(); 105 106 pst.setString(1, xref.getId()); 106 107 pst.setString(2, xref.getDataSource().getSystemCode()); … … 116 117 throw new IDMapperException (e); 117 118 } 119 finally {pst.cleanup(); } 118 120 return false; 119 121 } … … 126 128 private Map<String, String> getInfo() throws IDMapperException 127 129 { 130 final QueryLifeCycle pst = qInfo; 128 131 Map<String, String> result = new HashMap<String, String>(); 129 132 try 130 133 { 131 PreparedStatement pst = pstInfo.getPreparedStatement();134 pst.init(); 132 135 ResultSet rs = pst.executeQuery(); 133 136 … … 162 165 private String getBpInfo(Xref ref) throws IDMapperException 163 166 { 167 final QueryLifeCycle pst = qBackpage; 164 168 try { 165 PreparedStatement pst = pstBackpage.getPreparedStatement();169 pst.init(); 166 170 pst.setString (1, ref.getId()); 167 171 pst.setString (2, ref.getDataSource().getSystemCode()); … … 174 178 return result; 175 179 } catch (SQLException e) { throw new IDMapperException (e); } //Gene not found 180 finally {pst.cleanup(); } 176 181 } 177 182 … … 179 184 public Set<Xref> mapID (Xref idc, DataSource... resultDs) throws IDMapperException 180 185 { 186 final QueryLifeCycle pst = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode; 181 187 Set<Xref> refs = new HashSet<Xref>(); 182 188 … … 184 190 try 185 191 { 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(); 197 193 pst.setString(1, idc.getId()); 198 194 pst.setString(2, idc.getDataSource().getSystemCode()); 195 if (resultDs.length == 1) pst.setString(3, resultDs[0].getSystemCode()); 199 196 200 197 Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); … … 214 211 throw new IDMapperException (e); 215 212 } 213 finally {pst.cleanup(); } 216 214 217 215 return refs; … … 222 220 // Logger.log.trace("Fetching cross references by attribute: " + attrName + " = " + attrValue); 223 221 List<Xref> refs = new ArrayList<Xref>(); 224 222 final QueryLifeCycle pst = qRefsByAttribute; 225 223 try { 226 PreparedStatement pst = pstRefsByAttribute.getPreparedStatement();224 pst.init(); 227 225 pst.setString(1, attrName); 228 226 pst.setString(2, attrValue); … … 235 233 throw new IDMapperException (e); 236 234 } 235 finally {pst.cleanup(); } 237 236 // Logger.log.trace("End fetching cross references by attribute"); 238 237 return refs; … … 373 372 { 374 373 Set<Xref> result = new HashSet<Xref>(); 374 final QueryLifeCycle pst = qFreeSearch; 375 375 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(); 385 379 while(r.next()) { 386 380 String id = r.getString(1); … … 394 388 throw new IDMapperException(e); 395 389 } 390 finally {pst.cleanup(); } 396 391 return result; 397 392 } … … 530 525 { 531 526 Set<DataSource> result = new HashSet<DataSource>(); 527 final QueryLifeCycle pst = qDatasources; 532 528 try 533 529 { 534 PreparedStatement pst = pstDatasources.getPreparedStatement();530 pst.init(); 535 531 ResultSet rs = pst.executeQuery(); 536 532 while (rs.next()) … … 544 540 throw new IDMapperException(ignore); 545 541 } 542 finally {pst.cleanup(); } 546 543 return result; 547 544 } … … 585 582 { 586 583 Set<String> result = new HashSet<String>(); 584 final QueryLifeCycle pst = qAttribute; 587 585 588 586 if (ATTRIBUTES_FROM_BACKPAGE.containsKey(attrname)) … … 601 599 602 600 try { 603 PreparedStatement pst = pstAttribute.getPreparedStatement();601 pst.init(); 604 602 pst.setString (1, ref.getId()); 605 603 pst.setString (2, ref.getDataSource().getSystemCode()); … … 612 610 return result; 613 611 } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref + ", Attribute: " + attrname, e); } // Database unavailable 612 finally {pst.cleanup(); } 614 613 } 615 614 … … 619 618 { 620 619 Map<String, Set<String>> result = new HashMap<String, Set<String>>(); 620 final QueryLifeCycle pst = qAllAttributes; 621 621 622 622 String bpInfo = getBpInfo(ref); … … 637 637 638 638 try { 639 PreparedStatement pst = pstAllAttributes.getPreparedStatement();639 pst.init(); 640 640 pst.setString (1, ref.getId()); 641 641 pst.setString (2, ref.getDataSource().getSystemCode()); … … 658 658 return result; 659 659 } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable 660 finally {pst.cleanup(); } 660 661 } 661 662 … … 680 681 { 681 682 Map<Xref, String> result = new HashMap<Xref, String>(); 683 final QueryLifeCycle pst = (MATCH_ID.equals (attrType)) ? 684 qIdSearchWithAttributes : qAttributeSearch; 682 685 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); 687 687 pst.setString(1, "%" + query.toLowerCase() + "%"); 688 688 ResultSet r = pst.executeQuery(); … … 698 698 throw new IDMapperException (e); 699 699 } 700 finally {pst.cleanup(); } 700 701 return result; 701 702 } … … 705 706 { 706 707 Set<String> result = new HashSet<String>(); 708 final QueryLifeCycle pst = qAttributesSet; 707 709 try 708 710 { 709 PreparedStatement pst = pstAttributesSet.getPreparedStatement();711 pst.init(); 710 712 ResultSet rs = pst.executeQuery(); 711 713 while (rs.next()) … … 718 720 throw new IDMapperException(ignore); 719 721 } 722 finally {pst.cleanup(); } 720 723 return result; 721 724 } -
trunk/org.bridgedb.rdb/src/org/bridgedb/rdb/SimpleGdbImpl3.java
r308 r316 42 42 private static final int GDB_COMPAT_VERSION = 3; //Preferred schema version 43 43 44 private final SimpleGdb. LazyPst pstDatasources = new SimpleGdb.LazyPst(44 private final SimpleGdb.QueryLifeCycle qDatasources = new SimpleGdb.QueryLifeCycle( 45 45 "SELECT codeRight FROM link GROUP BY codeRight" 46 46 ); 47 private final SimpleGdb. LazyPst pstInfo = new SimpleGdb.LazyPst(47 private final SimpleGdb.QueryLifeCycle qInfo = new SimpleGdb.QueryLifeCycle( 48 48 "SELECT * FROM info" 49 49 ); 50 private final SimpleGdb. LazyPst pstXrefExists = new SimpleGdb.LazyPst(50 private final SimpleGdb.QueryLifeCycle qXrefExists = new SimpleGdb.QueryLifeCycle( 51 51 "SELECT id FROM " + "datanode" + " WHERE " + 52 52 "id = ? AND code = ?" 53 53 ); 54 private final SimpleGdb. LazyPst pstAttribute = new SimpleGdb.LazyPst(54 private final SimpleGdb.QueryLifeCycle qAttribute = new SimpleGdb.QueryLifeCycle( 55 55 "SELECT attrvalue FROM attribute " + 56 56 " WHERE id = ? AND code = ? AND attrname = ?" 57 57 ); 58 private final SimpleGdb. LazyPst pstAllAttributes = new SimpleGdb.LazyPst(58 private final SimpleGdb.QueryLifeCycle qAllAttributes = new SimpleGdb.QueryLifeCycle( 59 59 "SELECT attrname, attrvalue FROM attribute " + 60 60 " WHERE id = ? AND code = ?" 61 61 ); 62 private final SimpleGdb. LazyPst pstAttributesSet = new SimpleGdb.LazyPst(62 private final SimpleGdb.QueryLifeCycle qAttributesSet = new SimpleGdb.QueryLifeCycle( 63 63 "SELECT attrname FROM attribute GROUP BY attrname" 64 64 ); 65 private final SimpleGdb. LazyPst pstCrossRefs = new SimpleGdb.LazyPst(65 private final SimpleGdb.QueryLifeCycle qCrossRefs = new SimpleGdb.QueryLifeCycle ( 66 66 "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 67 67 "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 68 68 "WHERE src.idRight = ? AND src.codeRight = ?" 69 69 ); 70 private final SimpleGdb. LazyPst pstCrossRefsWithCode = new SimpleGdb.LazyPst(70 private final SimpleGdb.QueryLifeCycle qCrossRefsWithCode = new SimpleGdb.QueryLifeCycle ( 71 71 "SELECT dest.idRight, dest.codeRight FROM link AS src JOIN link AS dest " + 72 72 "ON src.idLeft = dest.idLeft and src.codeLeft = dest.codeLeft " + 73 73 "WHERE src.idRight = ? AND src.codeRight = ? AND dest.codeRight = ?" 74 74 ); 75 private final SimpleGdb. LazyPst pstRefsByAttribute = new SimpleGdb.LazyPst(75 private final SimpleGdb.QueryLifeCycle qRefsByAttribute = new SimpleGdb.QueryLifeCycle ( 76 76 "SELECT datanode.id, datanode.code FROM datanode " + 77 77 " LEFT JOIN attribute ON attribute.code = datanode.code AND attribute.id = datanode.id " + 78 78 "WHERE attrName = ? AND attrValue = ?" 79 79 ); 80 private final SimpleGdb. LazyPst pstFreeSearch = new SimpleGdb.LazyPst(80 private final SimpleGdb.QueryLifeCycle qFreeSearch = new SimpleGdb.QueryLifeCycle ( 81 81 "SELECT id, code FROM datanode WHERE " + 82 82 "LOWER(ID) LIKE ?" 83 83 ); 84 private final SimpleGdb. LazyPst pstAttributeSearch = new SimpleGdb.LazyPst(84 private final SimpleGdb.QueryLifeCycle qAttributeSearch = new SimpleGdb.QueryLifeCycle ( 85 85 "SELECT id, code, attrvalue FROM attribute WHERE " + 86 86 "attrname = 'Symbol' AND LOWER(attrvalue) LIKE ?" 87 87 ); 88 private final SimpleGdb. LazyPst pstIdSearchWithAttributes = new SimpleGdb.LazyPst(88 private final SimpleGdb.QueryLifeCycle qIdSearchWithAttributes = new SimpleGdb.QueryLifeCycle ( 89 89 "SELECT id, code, attrvalue FROM attribute WHERE " + 90 90 "attrname = 'Symbol' AND LOWER(ID) LIKE ?" 91 91 ); 92 92 93 93 /** {@inheritDoc} */ 94 94 public boolean xrefExists(Xref xref) throws IDMapperException 95 95 { 96 final QueryLifeCycle pst = qXrefExists; 96 97 try 97 98 { 98 PreparedStatement pst = pstXrefExists.getPreparedStatement();99 pst.init(); 99 100 pst.setString(1, xref.getId()); 100 101 pst.setString(2, xref.getDataSource().getSystemCode()); … … 110 111 throw new IDMapperException (e); 111 112 } 113 finally {pst.cleanup(); } 112 114 return false; 113 115 } … … 117 119 { 118 120 Set<Xref> refs = new HashSet<Xref>(); 119 121 final QueryLifeCycle pst = resultDs.length != 1 ? qCrossRefs : qCrossRefsWithCode; 120 122 try 121 123 { 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(); 133 125 pst.setString(1, idc.getId()); 134 126 pst.setString(2, idc.getDataSource().getSystemCode()); 127 if (resultDs.length == 1) 128 { 129 pst.setString(3, resultDs[0].getSystemCode()); 130 } 135 131 136 132 Set<DataSource> dsFilter = new HashSet<DataSource>(Arrays.asList(resultDs)); … … 150 146 throw new IDMapperException (e); 151 147 } 148 finally {pst.cleanup(); } 152 149 153 150 return refs; … … 159 156 List<Xref> refs = new ArrayList<Xref>(); 160 157 158 final QueryLifeCycle pst = qRefsByAttribute; 161 159 try { 162 PreparedStatement pst = pstRefsByAttribute.getPreparedStatement();160 pst.init(); 163 161 pst.setString(1, attrName); 164 162 pst.setString(2, attrValue); … … 171 169 throw new IDMapperException (e); 172 170 } 171 finally {pst.cleanup(); } 173 172 // Logger.log.trace("End fetching cross references by attribute"); 174 173 return refs; … … 308 307 { 309 308 Set<Xref> result = new HashSet<Xref>(); 309 final QueryLifeCycle pst = qFreeSearch; 310 310 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(); 320 314 while(r.next()) { 321 315 String id = r.getString(1); … … 329 323 throw new IDMapperException(e); 330 324 } 325 finally {pst.cleanup(); } 331 326 return result; 332 327 } … … 465 460 { 466 461 Map<String, String> result = new HashMap<String, String>(); 462 final QueryLifeCycle pst = qInfo; 467 463 try 468 464 { 469 PreparedStatement pst = pstInfo.getPreparedStatement();465 pst.init(); 470 466 ResultSet rs = pst.executeQuery(); 471 467 … … 496 492 { 497 493 Set<DataSource> result = new HashSet<DataSource>(); 494 final QueryLifeCycle pst = qDatasources; 498 495 try 499 496 { 500 PreparedStatement pst = pstDatasources.getPreparedStatement();497 pst.init(); 501 498 ResultSet rs = pst.executeQuery(); 502 499 while (rs.next()) … … 539 536 { 540 537 Set<String> result = new HashSet<String>(); 538 final QueryLifeCycle pst = qAttribute; 541 539 try { 542 PreparedStatement pst = pstAttribute.getPreparedStatement();540 pst.init(); 543 541 pst.setString (1, ref.getId()); 544 542 pst.setString (2, ref.getDataSource().getSystemCode()); … … 551 549 return result; 552 550 } catch (SQLException e) { throw new IDMapperException (e); } // Database unavailable 551 finally {pst.cleanup(); } 553 552 } 554 553 … … 558 557 { 559 558 Map<String, Set<String>> result = new HashMap<String, Set<String>>(); 559 final QueryLifeCycle pst = qAllAttributes; 560 560 try { 561 PreparedStatement pst = pstAllAttributes.getPreparedStatement();561 pst.init(); 562 562 pst.setString (1, ref.getId()); 563 563 pst.setString (2, ref.getDataSource().getSystemCode()); … … 580 580 return result; 581 581 } catch (SQLException e) { throw new IDMapperException ("Xref:" + ref, e); } // Database unavailable 582 finally {pst.cleanup(); } 582 583 } 583 584 … … 602 603 { 603 604 Map<Xref, String> result = new HashMap<Xref, String>(); 605 final QueryLifeCycle pst = (MATCH_ID.equals (attrType)) ? 606 qIdSearchWithAttributes : qAttributeSearch; 604 607 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); 609 609 pst.setString(1, "%" + query.toLowerCase() + "%"); 610 610 ResultSet r = pst.executeQuery(); … … 620 620 throw new IDMapperException (e); 621 621 } 622 finally {pst.cleanup(); } 622 623 return result; 623 624 } … … 627 628 { 628 629 Set<String> result = new HashSet<String>(); 630 final QueryLifeCycle pst = qAttributesSet; 629 631 try 630 632 { 631 PreparedStatement pst = pstAttributesSet.getPreparedStatement();633 pst.init(); 632 634 ResultSet rs = pst.executeQuery(); 633 635 while (rs.next()) … … 640 642 throw new IDMapperException(ignore); 641 643 } 644 finally {pst.cleanup(); } 642 645 return result; 643 646 }
