hibernate调用mysql存储过程

org.springframework.transaction.TransactionSystemException: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started
最近在写操作CLOB的过程中总是报这个异常,数据去插入进去了.


先看看SessionFactory.getCurrentSession与openSession的区别
1. 如果使用的是getCurrentSession来创建session的话,在commit后,session就自动被关闭了,
    也就是不用再session.close()了。但是如果使用的是openSession方法创建的session的话,
    那么必须显示的关闭session,也就是调用session.close()方法。这样commit后,session并没有关闭

开始用的:

Session session = this.getHibernateTemplate().getSessionFactory().getCurrentSession();
Transaction tran=session.beginTransaction();

好多文章里都说只需在Spring配置   中加入 thread就可以解决,试了没用.

修改后的:

Session session = this.getHibernateTemplate().getSessionFactory().openSession();

Transaction tran=session.beginTransaction();

......

finally
   {   
    session.close();   
   }

 

	public void updateStatistic() {
		// TODO Auto-generated method stub
		Session session = getSessionFactory().getCurrentSession();

		session.doWork(new Work() {
			public void execute(Connection connection) {
				// 这里已经得到connection了,可以继续你的JDBC代码。
				// 注意不要close了这个connection。

				CallableStatement cs;
				try {
					cs = connection
							.prepareCall("{call assessIndexStatistic_procedure}");
					cs.execute(); // 调用存储过程
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值