테스트

aion-server 4.8

Gitteol
최고관리자 · 1 · 💬 0 클론/새로받기
 4.8 61f661d · 1 commits 새로받기(Pull)
commons/src/com/aionemu/commons/database/Transaction.java
package com.aionemu.commons.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Savepoint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class allows easy manipulations with transactions, it should be used when critical or synchronized data should be commited to two or more
 * tables. This class allows us to avoid data synchronization problems in db.
 * <p/>
 * Class is not designed to be thread-safe, should be synchronized externally.<br>
 * Class is not fail-safe, if error happens - exception will be thrown.
 * 
 * @author SoulKeeper
 */
public class Transaction {

	/**
	 * Logger for transactions
	 */
	private static final Logger log = LoggerFactory.getLogger(Transaction.class);

	/**
	 * Connection that is allocated for this transaction
	 */
	private Connection connection;

	/**
	 * Package private constructor, should be instantiated via {@link com.aionemu.commons.database.DB#beginTransaction()} class
	 * 
	 * @param con
	 *          Connection that will be used for this transaction
	 * @throws java.sql.SQLException
	 *           if can't disable autocommit mode
	 */
	Transaction(Connection con) throws SQLException {
		this.connection = con;
		connection.setAutoCommit(false);
	}

	/**
	 * Adds Insert / Update Query to the transaction
	 * 
	 * @param sql
	 *          SQL string
	 * @throws SQLException
	 *           if something went wrong
	 */
	public void insertUpdate(String sql) throws SQLException {
		insertUpdate(sql, null);
	}

	/**
	 * Adds Insert / Update Query to this transaction. Utilizes IUSth for Batching and Query Editing. MUST MANUALLY EXECUTE QUERY / BATACH IN IUSth (No
	 * need to close Statement after execution)
	 * 
	 * @param sql
	 *          Sql query
	 * @param iusth
	 *          query helper
	 * @throws SQLException
	 *           if something went wrong
	 */
	public void insertUpdate(String sql, IUStH iusth) throws SQLException {
		PreparedStatement statement = connection.prepareStatement(sql);
		if (iusth != null) {
			iusth.handleInsertUpdate(statement);
		} else {
			statement.executeUpdate();
		}
	}

	/**
	 * Creates new savepoint
	 * 
	 * @param name
	 *          name of savepoint
	 * @return created savepoint
	 * @throws SQLException
	 *           if can't create save point
	 */
	public Savepoint setSavepoint(String name) throws SQLException {
		return connection.setSavepoint(name);
	}

	/**
	 * Releases savepoint of transaction
	 * 
	 * @param savepoint
	 *          savepoint to release
	 * @throws SQLException
	 *           if something went wrong
	 */
	public void releaseSavepoint(Savepoint savepoint) throws SQLException {
		connection.releaseSavepoint(savepoint);
	}

	/**
	 * Commits transaction
	 * 
	 * @throws SQLException
	 *           if something is wrong with transaction
	 */
	public void commit() throws SQLException {
		commit(null);
	}

	/**
	 * Commits transaction. If rollBackToOnError is null - whole transaction will be rolledback
	 * 
	 * @param rollBackToOnError
	 *          savepoint that should be used to rollback
	 * @throws SQLException
	 *           if something went wrongF
	 */
	public void commit(Savepoint rollBackToOnError) throws SQLException {

		try {
			connection.commit();
		} catch (SQLException e) {
			log.warn("Error while commiting transaction", e);

			try {
				if (rollBackToOnError != null) {
					connection.rollback(rollBackToOnError);
				} else {
					connection.rollback();
				}
			} catch (SQLException e1) {
				log.error("Can't rollback transaction", e1);
			}
		}

		connection.setAutoCommit(true);
		connection.close();
	}
}

📎 첨부파일

댓글 작성 권한이 없습니다.
🏆 포인트 랭킹 TOP 10
순위 닉네임 포인트
1 no_profile 타키야겐지쪽지보내기 자기소개 아이디로 검색 전체게시물 102,949
2 no_profile 동가리쪽지보내기 자기소개 아이디로 검색 전체게시물 63,733
3 no_profile 라프텔쪽지보내기 자기소개 아이디로 검색 전체게시물 51,771
4 no_profile 불멸의행복쪽지보내기 자기소개 아이디로 검색 전체게시물 36,923
5 서번트쪽지보내기 자기소개 아이디로 검색 전체게시물 35,011
6 no_profile 닥터스쪽지보내기 자기소개 아이디로 검색 전체게시물 29,470
7 no_profile 검은고양이쪽지보내기 자기소개 아이디로 검색 전체게시물 29,077
8 no_profile Revolution쪽지보내기 자기소개 아이디로 검색 전체게시물 28,199
9 no_profile 보거스쪽지보내기 자기소개 아이디로 검색 전체게시물 26,731
10 no_profile 호롤롤로쪽지보내기 자기소개 아이디로 검색 전체게시물 17,020
알림 0