iteye_8232 2009-04-16 16:24
浏览 127
已采纳

spring 事务

在做spring和hibernate进行整合的时候,进行单元测试的时候,发现这样的异常
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Cannot open connection
在控制台已经没有任何错误,不知道谁什么原因,在google了半天也没找到原因。谢谢大家!数据库采用的是mysql

[b]问题补充:[/b]
我把xml发给大家看看。。非 常感谢啊 有点长 呵呵<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

context:annotation-config/<!-- 把注解注册到spring容器中 -->


<!--驱动类 -->
<!--URL -->
<!--连接数据库用户名-->
<!--连接数据库密码-->
<!-- 连接池启动的初始值 -->
<!-- 连接池的最大值 -->
<!-- 最大空闲值 ,经过高峰期后,恢复时候-->
<!-- 最小空闲值-->


<!-- 把hibernate session给spring管理 -->
<!-- 匹配上面的数据源 -->


bean/Person.hbm.xml




hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true




<!-- 基于注解的方式申明事物 -->
[b]问题补充:[/b]
因为我现在用的单元测试 还没有用到web容器 tomcat应该和tomcat没多大关系吧 呵呵!
[b]问题补充:[/b]
应该不是测试类的问题吧 测试类是这样写的
private static PersonServiceInter bean;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext ac=new ClassPathXmlApplicationContext("springBean.xml");
bean=(PersonServiceInter)ac.getBean("[color=red]personservice[/color]");
}

@Test
public void testAdd() {
System.out.print("sss");
bean.add(new Person("jiba"));
}
personservice的类代码
@Transactional//注解方式申明事务
public class PersonService implements PersonServiceInter {
@Resource private SessionFactory sessionFactory; //用resource注解方式注入,一般都是这个方式
public void add(Person person){
sessionFactory.getCurrentSession().persist(person);//得到spring容器当前管理的session

//方法执行前打开事物,执行完后就自动关闭
}

  • 写回答

5条回答 默认 最新

  • wanghaolovezlq 2009-04-16 16:59
    关注

    那就可能你的组件里那个测试的类里没有注释事务吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable