1 22 package org.jboss.resource.adapter.jdbc; 23 24 import java.sql.ResultSet ; 25 import java.sql.SQLException ; 26 27 import org.jboss.logging.Logger; 28 import org.jboss.util.LRUCachePolicy; 29 30 38 public class PreparedStatementCache extends LRUCachePolicy 39 { 40 private final Logger log = Logger.getLogger(getClass()); 41 42 public static class Key 43 { 44 public static final int PREPARED_STATEMENT = 1; 45 public static final int CALLABLE_STATEMENT = 2; 46 private final String sql; 47 private final int type; 48 private final int resultSetType; 49 private final int resultSetConcurrency; 50 51 public Key(String sql, int type, int resultSetType, int resultSetConcurrency) 52 { 53 this.sql = sql; 54 this.type = type; 55 this.resultSetType = resultSetType; 56 this.resultSetConcurrency = resultSetConcurrency; 57 } 58 59 public boolean equals(Object o) 60 { 61 if (this == o) return true; 62 if (o == null || o instanceof Key == false) return false; 63 64 final Key key = (Key) o; 65 66 if (resultSetConcurrency != key.resultSetConcurrency) return false; 67 if (resultSetType != key.resultSetType) return false; 68 if (type != key.type) return false; 69 return !(sql != null ? !sql.equals(key.sql) : key.sql != null); 70 71 } 72 73 public int hashCode() 74 { 75 int result; 76 result = (sql != null ? sql.hashCode() : 0); 77 result = 29 * result + type; 78 result = 29 * result + resultSetType; 79 result = 29 * result + resultSetConcurrency; 80 return result; 81 } 82 public String toString() 83 { 84 StringBuffer tmp = new StringBuffer (super.toString()); 85 tmp.append('['); 86 tmp.append("sql="); 87 tmp.append(sql); 88 tmp.append(" type="); 89 tmp.append(type==PREPARED_STATEMENT ? "PS" : "CS"); 90 tmp.append(" resultSetType="); 91 switch (resultSetType) 92 { 93 case ResultSet.TYPE_FORWARD_ONLY: 94 { 95 tmp.append("TYPE_FORWARD_ONLY"); 96 break; 97 } 98 case ResultSet.TYPE_SCROLL_INSENSITIVE: 99 { 100 tmp.append("TYPE_SCROLL_INSENSITIVE"); 101 break; 102 } 103 case ResultSet.TYPE_SCROLL_SENSITIVE: 104 { 105 tmp.append("TYPE_SCROLL_SENSITIVE"); 106 break; 107 } 108 default: 109 tmp.append(resultSetType); 110 } 111 tmp.append(" resultSetConcurrency="); 112 switch (resultSetConcurrency) 113 { 114 case ResultSet.CONCUR_READ_ONLY: 115 { 116 tmp.append("CONCUR_READ_ONLY"); 117 break; 118 } 119 case ResultSet.CONCUR_UPDATABLE: 120 { 121 tmp.append("CONCUR_UPDATABLE"); 122 break; 123 } 124 default: 125 tmp.append(resultSetConcurrency); 126 } 127 tmp.append(']'); 128 return tmp.toString(); 129 } 130 } 131 132 public PreparedStatementCache(int max) 133 { 134 super(2, max); 135 create(); 136 } 137 138 protected void ageOut(LRUCachePolicy.LRUCacheEntry entry) 139 { 140 try 141 { 142 CachedPreparedStatement ws = (CachedPreparedStatement) entry.m_object; 143 ws.agedOut(); 144 } 145 catch (SQLException e) 146 { 147 log.debug("Failed closing cached statement", e); 148 } 149 finally 150 { 151 super.ageOut(entry); 152 } 153 } 154 } 155 | Popular Tags |