1 16 package com.ibatis.sqlmap.engine.mapping.statement; 17 18 import com.ibatis.sqlmap.client.event.RowHandler; 19 import com.ibatis.sqlmap.engine.cache.CacheKey; 20 import com.ibatis.sqlmap.engine.cache.CacheModel; 21 import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap; 22 import com.ibatis.sqlmap.engine.mapping.result.ResultMap; 23 import com.ibatis.sqlmap.engine.mapping.sql.Sql; 24 import com.ibatis.sqlmap.engine.scope.RequestScope; 25 import com.ibatis.sqlmap.engine.transaction.Transaction; 26 27 import java.sql.SQLException ; 28 import java.util.List ; 29 30 public class CachingStatement implements MappedStatement { 31 32 private MappedStatement statement; 33 private CacheModel cacheModel; 34 35 public CachingStatement(MappedStatement statement, CacheModel cacheModel) { 36 this.statement = statement; 37 this.cacheModel = cacheModel; 38 } 39 40 public String getId() { 41 return statement.getId(); 42 } 43 44 public StatementType getStatementType() { 45 return statement.getStatementType(); 46 } 47 48 public Integer getResultSetType() { 49 return statement.getResultSetType(); 50 } 51 52 public Integer getFetchSize() { 53 return statement.getFetchSize(); 54 } 55 56 public ParameterMap getParameterMap() { 57 return statement.getParameterMap(); 58 } 59 60 public ResultMap getResultMap() { 61 return statement.getResultMap(); 62 } 63 64 public int executeUpdate(RequestScope request, Transaction trans, Object parameterObject) 65 throws SQLException { 66 int n = statement.executeUpdate(request, trans, parameterObject); 67 return n; 68 } 69 70 public Object executeQueryForObject(RequestScope request, Transaction trans, Object parameterObject, Object resultObject) 71 throws SQLException { 72 CacheKey cacheKey = getCacheKey(request, parameterObject); 73 cacheKey.update("executeQueryForObject"); 74 Object object = cacheModel.getObject(cacheKey); 75 if (object == CacheModel.NULL_OBJECT){ 76 object = null; 78 }else if (object == null) { 79 synchronized (cacheModel.getLock(cacheKey)) { 80 object = statement.executeQueryForObject(request, trans, parameterObject, resultObject); 81 cacheModel.putObject(cacheKey, object); 82 } 83 } 84 return object; 85 } 86 87 public List executeQueryForList(RequestScope request, Transaction trans, Object parameterObject, int skipResults, int maxResults) 88 throws SQLException { 89 CacheKey cacheKey = getCacheKey(request, parameterObject); 90 cacheKey.update("executeQueryForList"); 91 cacheKey.update(skipResults); 92 cacheKey.update(maxResults); 93 Object listAsObject = cacheModel.getObject(cacheKey); 94 List list; 95 if(listAsObject == CacheModel.NULL_OBJECT){ 96 list = null; 98 }else if (listAsObject == null) { 99 synchronized (cacheModel.getLock(cacheKey)) { 100 list = statement.executeQueryForList(request, trans, parameterObject, skipResults, maxResults); 101 cacheModel.putObject(cacheKey, list); 102 } 103 }else{ 104 list = (List ) listAsObject; 105 } 106 return list; 107 } 108 109 public void executeQueryWithRowHandler(RequestScope request, Transaction trans, Object parameterObject, RowHandler rowHandler) 110 throws SQLException { 111 statement.executeQueryWithRowHandler(request, trans, parameterObject, rowHandler); 112 } 113 114 public CacheKey getCacheKey(RequestScope request, Object parameterObject) { 115 CacheKey key = statement.getCacheKey(request, parameterObject); 116 if (!cacheModel.isReadOnly() && !cacheModel.isSerialize()) { 117 key.update(request.getSession()); 118 } 119 return key; 120 } 121 122 public void setBaseCacheKey(int base) { 123 statement.setBaseCacheKey(base); 124 } 125 126 public void addExecuteListener(ExecuteListener listener) { 127 statement.addExecuteListener(listener); 128 } 129 130 public void notifyListeners() { 131 statement.notifyListeners(); 132 } 133 134 public void initRequest(RequestScope request) { 135 statement.initRequest(request); 136 } 137 138 public Sql getSql() { 139 return statement.getSql(); 140 } 141 142 public Class getParameterClass() { 143 return statement.getParameterClass(); 144 } 145 146 } 147 | Popular Tags |