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

 更新时间:2020年8月17日 09:46  点击:1712

前言

因工作需要将公司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存储过程内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

[!--infotagslink--]

相关文章

  • 金额阿拉伯数字转换为中文的存储过程

    Create Procedure AtoC @ChangeMoney Money as Set Nocount ON Declare @String1 char(20) Declare @String2 char(30) ...2016-11-25
  • PHP操作MSSQL存储过程修改用户密码

    存储过程在数据库的应用中我们用到的非常的多了,下面我们来看一篇关于PHP操作MSSQL存储过程修改用户密码的例子,具体的如下所示。 mssql2008 存储过程 下面可以直接...2016-11-25
  • 解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题

    这篇文章主要介绍了解决springboot使用logback日志出现LOG_PATH_IS_UNDEFINED文件夹的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-04-28
  • SpringBoot实现excel文件生成和下载

    这篇文章主要为大家详细介绍了SpringBoot实现excel文件生成和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-09
  • Oracle使用like查询时对下划线的处理方法

    这篇文章主要介绍了Oracle使用like查询时对下划线的处理方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-16
  • 详解springBoot启动时找不到或无法加载主类解决办法

    这篇文章主要介绍了详解springBoot启动时找不到或无法加载主类解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • SpringBoot集成Redis实现消息队列的方法

    这篇文章主要介绍了SpringBoot集成Redis实现消息队列的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
  • 解决Springboot get请求是参数过长的情况

    这篇文章主要介绍了解决Springboot get请求是参数过长的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-17
  • Springboot+TCP监听服务器搭建过程图解

    这篇文章主要介绍了Springboot+TCP监听服务器搭建过程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-10-28
  • MySql存储过程之逻辑判断和条件控制

    具体详情请看下文小编给大家带来的知识点。同编写程序类似,存储过程中也有对应的条件判断,功能类似于if、switch。在MySql里面对应的是IF和CASE1、IF判断IF判断的格式是这样的:IF expression THEN commands [ELSEIF ex...2015-10-21
  • Spring Boot项目@RestController使用重定向redirect方式

    这篇文章主要介绍了Spring Boot项目@RestController使用重定向redirect方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-02
  • springBoot 项目排除数据库启动方式

    这篇文章主要介绍了springBoot 项目排除数据库启动方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-10
  • springboot中使用@Transactional注解事物不生效的坑

    这篇文章主要介绍了springboot中使用@Transactional注解事物不生效的原因,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-26
  • SpringBoot接口接收json参数解析

    这篇文章主要介绍了SpringBoot接口接收json参数解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-19
  • Java连接数据库oracle中文乱码解决方案

    这篇文章主要介绍了Java连接数据库oracle中文乱码解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-16
  • 详解SpringBoot之访问静态资源(webapp...)

    这篇文章主要介绍了详解SpringBoot之访问静态资源(webapp...),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-14
  • PHP调用MySQL存储过程并返回值实现程序

    本文章来给大家详细介绍在php中如何来调用执行mysql存储过程然后返回由存储过程返回的值了,有需要了解的同学可进入参考。 。调用存储过程的方法。 a。如果存储过...2016-11-25
  • C#连接Oracle数据库字符串(引入DLL)的方式

    这篇文章主要给大家介绍了关于C#连接Oracle数据库字符串(引入DLL)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-06-25
  • springboot多模块包扫描问题的解决方法

    这篇文章主要介绍了springboot多模块包扫描问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-09-16
  • Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用详解

    这篇文章主要介绍了Springboot mybatis plus druid多数据源解决方案 dynamic-datasource的使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-18