Springboot呼叫Oracle儲存過程的幾種方式

Tiro8183發表於2020-08-11
  • 因工作需要將公司SSH專案改為Spingboot專案,將專案中部分需要呼叫儲存過程的部分用entityManagerFactory.unwrap(SessionFactory.class).openSession()來獲取Session實現後發現專案訪問資料庫超過十次就會掛掉,原因是Springboot連線池數量預設為10,猜測是每次訪問資料庫後連線未釋放導致的,手動關閉session後問題解決。
  • 解決問題的過程中又發現了另外兩種呼叫方式,
  1. 直接用EntityManager的createStoredProcedureQuery()方法呼叫 (推薦)
  2. 通過如下方式獲取Session來呼叫,這種方式不需要手動關閉Session來釋放連線,具體原因我也沒搞明白,有知道的朋友歡迎指點
    Session session = entityManager.unwrap(Session.class);
  • 完整程式碼
package com.hzjd.produre.repository;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.ParameterMode;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.procedure.ProcedureCall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hzjd.produre.bean.QueryResponse;
import com.hzjd.produre.utils.Assistant;

@Repository
public class ProdureDAO {
	public final static String PUBLIC_PAG_SYS_GETNEXTID = "PUBLIC_PAG.SYS_GETNEXTID";
	public final static String PSBC_QUERYBILL = "PSBCPAY.QUERYBILL";
	@PersistenceContext
	EntityManager entityManager;
	@Autowired
	EntityManagerFactory entityManagerFactory;

	public Session getSession() {
		return entityManagerFactory.unwrap(SessionFactory.class).openSession();
	}

	/**
	 * 使用entityManager呼叫儲存過程
	 * 
	 * @param pay_ID
	 * @return
	 */
	public QueryResponse queryBill1(String pay_ID) throws Exception {
		QueryResponse queryResponse = new QueryResponse();
		StoredProcedureQuery call = entityManager.createStoredProcedureQuery(PSBC_QUERYBILL);
		call.registerStoredProcedureParameter(1, String.class, ParameterMode.IN).setParameter(1, pay_ID);
		call.registerStoredProcedureParameter(2, String.class, ParameterMode.OUT);
		call.registerStoredProcedureParameter(3, String.class, ParameterMode.OUT);
		call.registerStoredProcedureParameter(4, String.class, ParameterMode.OUT);
		call.registerStoredProcedureParameter(5, String.class, ParameterMode.OUT);
		call.registerStoredProcedureParameter(6, String.class, ParameterMode.OUT);
		call.registerStoredProcedureParameter(7, String.class, ParameterMode.OUT);
		call.registerStoredProcedureParameter(8, String.class, ParameterMode.OUT);
		call.registerStoredProcedureParameter(9, String.class, ParameterMode.OUT);
		call.registerStoredProcedureParameter(10, String.class, ParameterMode.OUT);
		call.execute();
		queryResponse.getBody().setPAY_ID(pay_ID);
		queryResponse.getBody().setCUSTNAME(Assistant.nullToEmpty(call.getOutputParameterValue(2)));
		queryResponse.getBody().setHOME_ADDR(Assistant.nullToEmpty(call.getOutputParameterValue(3)));
		queryResponse.getBody().setTRAN_AMT(Assistant.nullToEmpty(call.getOutputParameterValue(5)));
		queryResponse.getBody().setTOTAL_AMT(Assistant.nullToEmpty(call.getOutputParameterValue(6)));
		queryResponse.getBody().setBALANCE(Assistant.nullToEmpty(call.getOutputParameterValue(8)));
		int errorcode = Assistant.nullToInt(call.getOutputParameterValue(9));
		String errormsg = Assistant.nullToEmpty(call.getOutputParameterValue(10));
		if (errorcode == 0) {
			return queryResponse;
		} else {
			throw new Exception(errormsg);
		}
	}

	/**
	 * 使用sessionFactory開啟Session呼叫儲存過程
	 * 
	 * @param pay_ID
	 * @return
	 */
	public QueryResponse queryBill2(String pay_ID) throws Exception {
		QueryResponse queryResponse = new QueryResponse();
		// 呼叫完成後需關閉Session否則會出現連線失效
		try (Session session = getSession();) {
			ProcedureCall call = session.createStoredProcedureCall(PSBC_QUERYBILL);
			call.registerParameter(1, String.class, ParameterMode.IN).bindValue(pay_ID);
			call.registerParameter(2, String.class, ParameterMode.OUT);
			call.registerParameter(3, String.class, ParameterMode.OUT);
			call.registerParameter(4, String.class, ParameterMode.OUT);
			call.registerParameter(5, String.class, ParameterMode.OUT);
			call.registerParameter(6, String.class, ParameterMode.OUT);
			call.registerParameter(7, String.class, ParameterMode.OUT);
			call.registerParameter(8, String.class, ParameterMode.OUT);
			call.registerParameter(9, String.class, ParameterMode.OUT);
			call.registerParameter(10, String.class, ParameterMode.OUT);
			queryResponse.getBody().setPAY_ID(pay_ID);
			queryResponse.getBody().setCUSTNAME(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(2)));
			queryResponse.getBody().setHOME_ADDR(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(3)));
			queryResponse.getBody().setTRAN_AMT(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(5)));
			queryResponse.getBody().setTOTAL_AMT(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(6)));
			queryResponse.getBody().setBALANCE(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(8)));
			int errorcode = Assistant.nullToInt(call.getOutputs().getOutputParameterValue(9));
			String errormsg = Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(10));
			if (errorcode == 0) {
				return queryResponse;
			} else {
				throw new Exception(errormsg);
			}
		}
	}

	/**
	 * 使用sessionFactory開啟Session呼叫儲存過程
	 * 
	 * @param pay_ID
	 * @return
	 */
	public QueryResponse queryBill3(String pay_ID) throws Exception {
		QueryResponse queryResponse = new QueryResponse();
		Session session = entityManager.unwrap(Session.class);
		ProcedureCall call = session.createStoredProcedureCall(PSBC_QUERYBILL);
		call.registerParameter(1, String.class, ParameterMode.IN).bindValue(pay_ID);
		call.registerParameter(2, String.class, ParameterMode.OUT);
		call.registerParameter(3, String.class, ParameterMode.OUT);
		call.registerParameter(4, String.class, ParameterMode.OUT);
		call.registerParameter(5, String.class, ParameterMode.OUT);
		call.registerParameter(6, String.class, ParameterMode.OUT);
		call.registerParameter(7, String.class, ParameterMode.OUT);
		call.registerParameter(8, String.class, ParameterMode.OUT);
		call.registerParameter(9, String.class, ParameterMode.OUT);
		call.registerParameter(10, String.class, ParameterMode.OUT);
		queryResponse.getBody().setPAY_ID(pay_ID);
		queryResponse.getBody().setCUSTNAME(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(2)));
		queryResponse.getBody().setHOME_ADDR(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(3)));
		queryResponse.getBody().setTRAN_AMT(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(5)));
		queryResponse.getBody().setTOTAL_AMT(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(6)));
		queryResponse.getBody().setBALANCE(Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(8)));
		int errorcode = Assistant.nullToInt(call.getOutputs().getOutputParameterValue(9));
		String errormsg = Assistant.nullToEmpty(call.getOutputs().getOutputParameterValue(10));
		if (errorcode == 0) {
			return queryResponse;
		} else {
			throw new Exception(errormsg);
		}
	}
}

相關文章