日期:2014-05-16 浏览次数:20399 次
MyProprietaryConnection conn = ConnectionManager.checkOut(Database.DB_NAME); try { PreparedStatement stmt = conn.getPreparedStatement("some statement id, identifying a sql statement in an xml file"); stmt.setString(1, "param 1"); stmt.setInt(2, param2); ... try { ResultSet resultSet = stmt.executeQuery(); try{ while(resultSet.next()) { ... } } finally { resultSet.close(); } } finally { stmt.close(); } } finally { ConnectionManager.checkIn(conn); }
Object cacheKey = ...; synchronized(cache) { Account acct = (Account)cache.get(cacheKey); if(acct == null) { acct = runJdbcForAccount(...); cache.put(cacheKey, acct); } return acct.cloneAccount(); }
abstract class PersisterCommand { protected abstract void populateStatement(PreparedStatement stmt); protected abstract Object processResult(ResultSet resultSet); protected abstract boolean isTransactional(); protected abstract PreparedStatement getStatement(); public Object run() { MyProprietaryConnection conn = ConnectionManager.checkOut(Database.DB_NAME); try { PreparedStatement stmt = getStatement(); populateStatement(stmt); ... try { if(isTransactional()) { conn.startTransaction(); } ResultSet resultSet = stmt.executeQuery(); try{ Object result = processResult(resultSet); if(isTransactional()) { conn.commitTransaction(); } return result; } catch(Exception e){ if(isTransactional()) conn.rollbackTransaction(); throw e; } finally { resultSet.close(); } } finally { stmt.close(); } } finally { ConnectionManager.checkIn(conn); } } }
public interface Action { Object run(SqlMapSession session); } public class IbatisPersistence { public SqlMapSession openSession(); public Object queryForObject(String key); public List queryForList(String key); public int update(String key, boolean useTransaction); public int delete(String key, boolean useTransaction); public int update(String key); public int delete(String key); public Object run(Action action); }
Accunt acct = persistence.queryForObject("getAccountById", accountId);