📄 sqlmapexecutordelegate.java
字号:
/*
* Copyright 2004 Clinton Begin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibatis.sqlmap.engine.impl;
import com.ibatis.common.beans.Probe;
import com.ibatis.common.beans.ProbeFactory;
import com.ibatis.common.jdbc.exception.NestedSQLException;
import com.ibatis.common.util.PaginatedList;
import com.ibatis.common.util.ThrottledPool;
import com.ibatis.sqlmap.client.SqlMapException;
import com.ibatis.sqlmap.client.event.RowHandler;
import com.ibatis.sqlmap.engine.cache.CacheKey;
import com.ibatis.sqlmap.engine.cache.CacheModel;
import com.ibatis.sqlmap.engine.exchange.DataExchangeFactory;
import com.ibatis.sqlmap.engine.execution.BatchException;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
import com.ibatis.sqlmap.engine.mapping.result.ResultObjectFactory;
import com.ibatis.sqlmap.engine.mapping.statement.InsertStatement;
import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
import com.ibatis.sqlmap.engine.mapping.statement.PaginatedDataList;
import com.ibatis.sqlmap.engine.mapping.statement.SelectKeyStatement;
import com.ibatis.sqlmap.engine.scope.RequestScope;
import com.ibatis.sqlmap.engine.scope.SessionScope;
import com.ibatis.sqlmap.engine.transaction.Transaction;
import com.ibatis.sqlmap.engine.transaction.TransactionException;
import com.ibatis.sqlmap.engine.transaction.TransactionManager;
import com.ibatis.sqlmap.engine.transaction.TransactionState;
import com.ibatis.sqlmap.engine.transaction.user.UserProvidedTransaction;
import com.ibatis.sqlmap.engine.type.TypeHandlerFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* The workhorse that really runs the SQL
*/
public class SqlMapExecutorDelegate {
private static final Probe PROBE = ProbeFactory.getProbe();
/**
* The default maximum number of requests
*/
public static final int DEFAULT_MAX_REQUESTS = 512;
/**
* The default maximum number of sessions
*/
public static final int DEFAULT_MAX_SESSIONS = 128;
/**
* The default maximum number of transactions
*/
public static final int DEFAULT_MAX_TRANSACTIONS = 32;
private boolean lazyLoadingEnabled;
private boolean cacheModelsEnabled;
private boolean enhancementEnabled;
private int maxRequests = DEFAULT_MAX_REQUESTS;
private int maxSessions = DEFAULT_MAX_SESSIONS;
private int maxTransactions = DEFAULT_MAX_TRANSACTIONS;
private TransactionManager txManager;
private HashMap mappedStatements;
private HashMap cacheModels;
private HashMap resultMaps;
private HashMap parameterMaps;
private ThrottledPool requestPool;
private ThrottledPool sessionPool;
protected SqlExecutor sqlExecutor;
private TypeHandlerFactory typeHandlerFactory;
private DataExchangeFactory dataExchangeFactory;
private ResultObjectFactory resultObjectFactory;
private boolean statementCacheEnabled;
/**
* Default constructor
*/
public SqlMapExecutorDelegate() {
mappedStatements = new HashMap();
cacheModels = new HashMap();
resultMaps = new HashMap();
parameterMaps = new HashMap();
requestPool = new ThrottledPool(RequestScope.class, DEFAULT_MAX_REQUESTS);
sessionPool = new ThrottledPool(SessionScope.class, DEFAULT_MAX_SESSIONS);
sqlExecutor = new SqlExecutor();
typeHandlerFactory = new TypeHandlerFactory();
dataExchangeFactory = new DataExchangeFactory(typeHandlerFactory);
}
/**
* Getter for the DataExchangeFactory
*
* @return - the DataExchangeFactory
*/
public DataExchangeFactory getDataExchangeFactory() {
return dataExchangeFactory;
}
/**
* Getter for the TypeHandlerFactory
*
* @return - the TypeHandlerFactory
*/
public TypeHandlerFactory getTypeHandlerFactory() {
return typeHandlerFactory;
}
/**
* Getter for the status of lazy loading
*
* @return - the status
*/
public boolean isLazyLoadingEnabled() {
return lazyLoadingEnabled;
}
/**
* Turn on or off lazy loading
*
* @param lazyLoadingEnabled - the new state of caching
*/
public void setLazyLoadingEnabled(boolean lazyLoadingEnabled) {
this.lazyLoadingEnabled = lazyLoadingEnabled;
}
/**
* Getter for the status of caching
*
* @return - the status
*/
public boolean isCacheModelsEnabled() {
return cacheModelsEnabled;
}
/**
* Turn on or off caching
*
* @param cacheModelsEnabled - the new state of caching
*/
public void setCacheModelsEnabled(boolean cacheModelsEnabled) {
this.cacheModelsEnabled = cacheModelsEnabled;
}
/**
* Getter for the status of CGLib enhancements
*
* @return - the status
*/
public boolean isEnhancementEnabled() {
return enhancementEnabled;
}
/**
* Turn on or off CGLib enhancements
*
* @param enhancementEnabled - the new state
*/
public void setEnhancementEnabled(boolean enhancementEnabled) {
this.enhancementEnabled = enhancementEnabled;
}
/**
* Getter for the maximum number of requests
*
* @return - the maximum number of requests
*/
public int getMaxRequests() {
return maxRequests;
}
/**
* Setter for the maximum number of requests
*
* @param maxRequests - the maximum number of requests
*/
public void setMaxRequests(int maxRequests) {
this.maxRequests = maxRequests;
requestPool = new ThrottledPool(RequestScope.class, maxRequests);
}
/**
* Getter for the maximum number of sessions
*
* @return - the maximum number of sessions
*/
public int getMaxSessions() {
return maxSessions;
}
/**
* Setter for the maximum number of sessions
*
* @param maxSessions - the maximum number of sessions
*/
public void setMaxSessions(int maxSessions) {
this.maxSessions = maxSessions;
this.sessionPool = new ThrottledPool(SessionScope.class, maxSessions);
}
/**
* Getter for the the maximum number of transactions
*
* @return - the maximum number of transactions
*/
public int getMaxTransactions() {
return maxTransactions;
}
/**
* Setter for the maximum number of transactions
*
* @param maxTransactions - the maximum number of transactions
*/
public void setMaxTransactions(int maxTransactions) {
this.maxTransactions = maxTransactions;
}
/**
* Getter for the transaction manager
*
* @return - the transaction manager
*/
public TransactionManager getTxManager() {
return txManager;
}
/**
* Setter for the transaction manager
*
* @param txManager - the transaction manager
*/
public void setTxManager(TransactionManager txManager) {
this.txManager = txManager;
}
/**
* Add a mapped statement
*
* @param ms - the mapped statement to add
*/
public void addMappedStatement(MappedStatement ms) {
if (mappedStatements.containsKey(ms.getId())) {
throw new SqlMapException("There is already a statement named " + ms.getId() + " in this SqlMap.");
}
ms.setBaseCacheKey(hashCode());
mappedStatements.put(ms.getId(), ms);
}
/**
* Get an iterator of the mapped statements
*
* @return - the iterator
*/
public Iterator getMappedStatementNames() {
return mappedStatements.keySet().iterator();
}
/**
* Get a mappedstatement by its ID
*
* @param id - the statement ID
* @return - the mapped statement
*/
public MappedStatement getMappedStatement(String id) {
MappedStatement ms = (MappedStatement) mappedStatements.get(id);
if (ms == null) {
throw new SqlMapException("There is no statement named " + id + " in this SqlMap.");
}
return ms;
}
/**
* Add a cache model
*
* @param model - the model to add
*/
public void addCacheModel(CacheModel model) {
cacheModels.put(model.getId(), model);
}
/**
* Get an iterator of the cache models
*
* @return - the cache models
*/
public Iterator getCacheModelNames() {
return cacheModels.keySet().iterator();
}
/**
* Get a cache model by ID
*
* @param id - the ID
* @return - the cache model
*/
public CacheModel getCacheModel(String id) {
CacheModel model = (CacheModel) cacheModels.get(id);
if (model == null) {
throw new SqlMapException("There is no cache model named " + id + " in this SqlMap.");
}
return model;
}
/**
* Add a result map
*
* @param map - the result map to add
*/
public void addResultMap(ResultMap map) {
resultMaps.put(map.getId(), map);
}
/**
* Get an iterator of the result maps
*
* @return - the result maps
*/
public Iterator getResultMapNames() {
return resultMaps.keySet().iterator();
}
/**
* Get a result map by ID
*
* @param id - the ID
* @return - the result map
*/
public ResultMap getResultMap(String id) {
ResultMap map = (ResultMap) resultMaps.get(id);
if (map == null) {
throw new SqlMapException("There is no result map named " + id + " in this SqlMap.");
}
return map;
}
/**
* Add a parameter map
*
* @param map - the map to add
*/
public void addParameterMap(ParameterMap map) {
parameterMaps.put(map.getId(), map);
}
/**
* Get an iterator of all of the parameter maps
*
* @return - the parameter maps
*/
public Iterator getParameterMapNames() {
return parameterMaps.keySet().iterator();
}
/**
* Get a parameter map by ID
*
* @param id - the ID
* @return - the parameter map
*/
public ParameterMap getParameterMap(String id) {
ParameterMap map = (ParameterMap) parameterMaps.get(id);
if (map == null) {
throw new SqlMapException("There is no parameter map named " + id + " in this SqlMap.");
}
return map;
}
/**
* Flush all of the data caches
*/
public void flushDataCache() {
Iterator models = cacheModels.values().iterator();
while (models.hasNext()) {
((CacheModel) models.next()).flush();
}
}
/**
* Flush a single cache by ID
*
* @param id - the ID
*/
public void flushDataCache(String id) {
CacheModel model = getCacheModel(id);
if (model != null) {
model.flush();
}
}
//-- Basic Methods
/**
* Call an insert statement by ID
*
* @param session - the session
* @param id - the statement ID
* @param param - the parameter object
* @return - the generated key (or null)
* @throws SQLException - if the insert fails
*/
public Object insert(SessionScope session, String id, Object param) throws SQLException {
Object generatedKey = null;
MappedStatement ms = getMappedStatement(id);
Transaction trans = getTransaction(session);
boolean autoStart = trans == null;
try {
trans = autoStartTransaction(session, autoStart, trans);
SelectKeyStatement selectKeyStatement = null;
if (ms instanceof InsertStatement) {
selectKeyStatement = ((InsertStatement) ms).getSelectKeyStatement();
}
if (selectKeyStatement != null && !selectKeyStatement.isAfter()) {
generatedKey = executeSelectKey(session, trans, ms, param);
}
RequestScope request = popRequest(session, ms);
try {
ms.executeUpdate(request, trans, param);
} finally {
pushRequest(request);
}
if (selectKeyStatement != null && selectKeyStatement.isAfter()) {
generatedKey = executeSelectKey(session, trans, ms, param);
}
autoCommitTransaction(session, autoStart);
} finally {
autoEndTransaction(session, autoStart);
}
return generatedKey;
}
private Object executeSelectKey(SessionScope session, Transaction trans, MappedStatement ms, Object param) throws SQLException {
Object generatedKey = null;
RequestScope request;
InsertStatement insert = (InsertStatement) ms;
SelectKeyStatement selectKeyStatement = insert.getSelectKeyStatement();
if (selectKeyStatement != null) {
request = popRequest(session, selectKeyStatement);
try {
generatedKey = selectKeyStatement.executeQueryForObject(request, trans, param, null);
String keyProp = selectKeyStatement.getKeyProperty();
if (keyProp != null) {
PROBE.setObject(param, keyProp, generatedKey);
}
} finally {
pushRequest(request);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -