📄 transaction.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package ActiveObject.core;import ActiveObject.vo.ObjectInfo;import ActiveObject.exception.*;import java.sql.*;import java.util.*;/** * * @author tanjiazhang */public class Transaction extends Task { private Connection connection = null; private Map<String, PreparedStatement> statements = new HashMap<String, PreparedStatement>(); protected Transaction(Connection connection) throws SQLException { this.connection = connection; connection.setAutoCommit(false);//显式声明事务 } /** * 保存一个对象,但不会令该对象成为持久化对象 * @param record * @return * @throws java.lang.IllegalAccessException * @throws java.lang.IllegalArgumentException * @throws java.lang.reflect.InvocationTargetException * @throws java.sql.SQLException */ public Transaction save(ActiveRecord record) throws ObjectAnalysisException, SQLException { ObjectInfo objectInfo = ActiveRecordHelper.analizeObjectInfo(record.getClass()); String key = createSaveKey(record); PreparedStatement stmt = statements.get(key); if(stmt == null) { stmt = connection.prepareStatement(objectInfo.getBasicSqlStatement().getInsertStatement()); statements.put(key, stmt); } objectInfo.setupSaveParams(stmt, record); stmt.addBatch(); return this; } public Transaction update(ActiveRecord record) throws ObjectAnalysisException, ObjectNotActive, SQLException { if(record.isActive == false) throw new ObjectNotActive("The argument is not a active object"); if(record.isActive == false) throw new ObjectNotActive("This is not a active object"); ObjectInfo objectInfo = ActiveRecordHelper.analizeObjectInfo(record.getClass()); String key = createUpdateKey(record); PreparedStatement stmt = statements.get(key); if(stmt == null) { stmt = connection.prepareStatement(objectInfo.getBasicSqlStatement().getUpdateStatement()); statements.put(key, stmt); } objectInfo.setupUpdateParams(stmt, record); stmt.addBatch(); return this; } public Transaction delete(ActiveRecord record) throws ObjectAnalysisException, ObjectNotActive, SQLException { if(record.isActive == false) throw new ObjectNotActive(); ObjectInfo objectInfo = ActiveRecordHelper.analizeObjectInfo(record.getClass()); String key = createDeleteKey(record); PreparedStatement stmt = this.statements.get(key); if(stmt == null) { stmt = this.connection.prepareStatement(objectInfo.getBasicSqlStatement().getDeleteStatement()); this.statements.put(key, stmt); } objectInfo.setupDeleteParams(stmt, record); stmt.addBatch(); return this; } /** * 执行批量操作,需要手动关闭 * @throws java.sql.SQLException */ public void execute() throws SQLException { try { for(String ket : statements.keySet()) { statements.get(ket).executeBatch(); } connection.commit(); } catch(SQLException err) { connection.rollback(); throw err; } } /** * 关闭batch并释放资源 * @throws java.sql.SQLException */ public void close() throws SQLException { connection.close(); this.statements = null; } /** * Execute the batch and then close it * @throws java.sql.SQLException */ public final void finish() throws SQLException { execute(); close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -