springboot中手動提交事務transaction
本次是主工程循環調用模塊檢索sequence執行nextval(),sequence更新沒有提交,所以多次取到相同值,這時需要給sequence取得手動提交。每一次循環取得的結果就加一了。
CommonUtils.java
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.message.MessageId;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapper.ComDocreqNumSeqMapper;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapper.ComOwntrdIdxSeasonSeqMapper;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapper.ComOwntrdIdxSelectSeqMapper;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapper.ComPlanMasterMapper;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapper.ComReceptNumSeqMapper;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapper.ComRecordNumSeqMapper;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapper.ComStaffMapper;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapperentity.ComConfliEntity;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapperentity.ComPlanMasterEntity;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapperentity.ComSeatxxInfEntity;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapperentity.ComStaffEntity;
import jp.co.pia.ticket.sports.hawks.seasonseat.common.util.mapperentity.ComStaffInfoEntity;
import lombok.extern.slf4j.Slf4j;
/**
* ファイル名 :CommonUtils <br>
* 機能概要:共通部品 <br>
* @author 日立ソリューションズ
* @version 1.0
*/
@Service
@Slf4j
@Scope("session")
public class CommonUtils {
/** 年間シートプランマスタMapper */
@Autowired
private ComPlanMasterMapper comPlanMasterMapper;
/** スタッフMapper */
@Autowired
private ComStaffMapper comStaffMapper;
/** 共通メッセージ */
@Autowired
private MessageSource msgSrc;
/** 受付番號シーケンス */
@Autowired
private ComReceptNumSeqMapper comReceptNumSeqMapper;
/** シーズンシート・Vシート用シーケンス */
@Autowired
private ComOwntrdIdxSeasonSeqMapper comOwntrdIdxSeasonSeqMapper;
/** パッケージセレクト・Vシート用シーケンス */
@Autowired
private ComOwntrdIdxSelectSeqMapper comOwntrdIdxSelectSeqMapper;
/** 資料請求ID用シーケンス */
@Autowired
private ComDocreqNumSeqMapper comDocreqNumSeqMapper;
/** 発注指示ID用シーケンス */
@Autowired
private ComRecordNumSeqMapper comRecordNumSeqMapper;
/**
* メソッド名:getIdValue<br>
* 機能概要:採番処理 <br>
* @param 引數1:ID種別
* @param 引數2:サブコード
* @return String 採番
* @throws Exception システム例外
*/
public String getIdValue(String numericTyp, String subcode) throws Exception {
String idValue = null;
long idValueLong = 0l;
TransactionStatus status = null;
try {
// トランザクション制御
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
status = transactionManager.getTransaction(definition);
if (UtilConst.STR_01.equals(numericTyp)) {
// ID種別 = '01'(受付番號)
String tableName = UtilConst.STR_RECEPT_NUM_SEQ+subcode;
idValueLong = comReceptNumSeqMapper.selectReceptNumSeq(tableName);
idValue = String.format(UtilConst.ZERO_09, idValueLong);
}
if (UtilConst.STR_02.equals(numericTyp) && UtilConst.STR_1.equals(subcode)) {
// ID種別 = '02' ,サブコード = '1'の場合, オーナーサイト取引ID(シーズンシートまたはVシート対象)
idValueLong = comOwntrdIdxSeasonSeqMapper.selectOwntrdIdxSeasonSeq();
idValue = String.format(UtilConst.ZERO_08, idValueLong);
}
if (UtilConst.STR_02.equals(numericTyp) && UtilConst.STR_7.equals(subcode)) {
// ID種別 = '02' ,サブコード = '7'の場合,オーナーサイト取引ID(パッケージセレクトまたはVセレクト対象)
idValueLong = comOwntrdIdxSelectSeqMapper.selectOwntrdIdxSelectSeq();
idValue = String.format(UtilConst.ZERO_08, idValueLong);
}
if (UtilConst.STR_03.equals(numericTyp)) {
// ID種別 = '03'(資料請求ID)
idValueLong = comDocreqNumSeqMapper.selectDocreqNumSeq();
idValue = String.format(UtilConst.ZERO_08, idValueLong);
}
if (UtilConst.STR_04.equals(numericTyp)) {
// ID種別 = '04'(発注指示ID)
idValueLong = comRecordNumSeqMapper.selectRecordNumSeq();
idValue = String.format(UtilConst.ZERO_10, idValueLong);
}
// コミット
transactionManager.commit(status);
} catch (DataAccessException e) {
SQLException se = (SQLException)e.getCause();
String sqlstate = se.getSQLState();
if(UtilConst.STR_SQLSTATE_SEQUENCE_OVERFLOW.equals(sqlstate)) {
// 採番結果値が採番最大値を超えた場合
String messagestr = msgSrc.getMessage(MessageId.EG000052, new String[] {numericTyp, subcode}, Locale.JAPAN);
log.error(messagestr);
// 例外発生時にはトランザクションをロールバック
transactionManager.rollback(status);
throw new Exception(messagestr);
}else {
// 例外発生時にはトランザクションをロールバック
transactionManager.rollback(status);
throw new Exception(e.getMessage());
}
} finally {
if (!status.isCompleted()) {
// 最後のチャンクについて、トランザクションをコミットする。
transactionManager.commit(status);
}
}
return idValue;
}
}