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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler