欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring boot调用Oracle存储过程的两种方式及完整代码

程序员文章站 2024-01-08 22:49:46
前言因工作需要将公司ssh项目改为spingboot项目,将项目中部分需要调用存储过程的部分用entitymanagerfactory.unwrap(sessionfactory.class).ope...

前言

因工作需要将公司ssh项目改为spingboot项目,将项目中部分需要调用存储过程的部分用entitymanagerfactory.unwrap(sessionfactory.class).opensession()来获取session实现后发现项目访问数据库超过十次就会挂掉,原因是springboot连接池数量默认为10,猜测是每次访问数据库后连接未释放导致的,手动关闭session后问题解决。

解决问题的过程中又发现了另外两种调用方式:

  • 直接用entitymanager的createstoredprocedurequery()方法调用 (推荐)
  • 通过如下方式获取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);
		}
	}
}

总结

到此这篇关于spring boot调用oracle存储过程的两种方式及完整代码的文章就介绍到这了,更多相关springboot调用oracle存储过程内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

上一篇:

下一篇: