第一个方法
public void addLog(Log log) {
Session session = sessionFactory.getCurrentSession();
session.save(log);
}
第二个方法
public void addUser(User user) {
Session session = sessionFactory.getCurrentSession();
session.save(user);
}
调用上面两个方法
@Transactional
public void addUser(User user) { userDAO.addUser(user);
logDAO.addLog(new Log());
}
运行测试:
@Test
public void testAspect()
{
ApplicationContext ctx = new FileSystemXmlApplicationContext("src\\applicationContext.xml");
UserService userService = (UserService)ctx.getBean("userService");
userService.addUser(new User());
}我已经加了@Transactional啊,怎么老是出错:No Hibernate Session bound to thread, and configuration does not allow creat
public void addLog(Log log) {
Session session = sessionFactory.getCurrentSession();
session.save(log);
}
第二个方法
public void addUser(User user) {
Session session = sessionFactory.getCurrentSession();
session.save(user);
}
调用上面两个方法
@Transactional
public void addUser(User user) { userDAO.addUser(user);
logDAO.addLog(new Log());
}
运行测试:
@Test
public void testAspect()
{
ApplicationContext ctx = new FileSystemXmlApplicationContext("src\\applicationContext.xml");
UserService userService = (UserService)ctx.getBean("userService");
userService.addUser(new User());
}我已经加了@Transactional啊,怎么老是出错:No Hibernate Session bound to thread, and configuration does not allow creat
解决方案 »
- 关于异常的一个问题
- spring中如何把log4j切片到service或者dao中?
- 求教HQL连接查询!~~
- 服务器暴崩,问题不解决,饭碗难保!
- 客户端连接Web Service遇到Connection timed out
- 散分!明天就可以和慧通的南京分公司签约了,有了解的谈谈这个公司的情况吧!
- struts如何与showModalDialog 方式结合使用
- 下载,《J2EE完全参考手册-J2EE概述》、《J2EE部署》
- 那位兄台能否告诉我如何crack Together Edition for JBuilder !(本人纯属学习之用)
- 测试EJB时,为何出现下面错误?org.omg.CORBA.OBJECT_NOT_EXIST
- myBatis执行带输出参数的存储过程问题
- 刚学struts2,出现错误,搜索后依然不会解决,麻烦大家帮忙看下
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
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></context:annotation-config>
<context:component-scan base-package="spring"></context:component-scan> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/webdb"/>
<property name="username" value="root"/>
<property name="password" value="19830112"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>entity.User</value>
<value>entity.Log</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
但是仍然有事物不能回滚的问题
希望有朋友可以来帮忙解答下
让第一个方法抛出运行时异常
public void addLog(Log log) {
Session session = sessionFactory.getCurrentSession();
session.save(log);
throw new RuntimeException("运行异常");
}
再次运行测试,发现成功向数据库中添加数据,事务并没有回滚
这个问题我也是研究了好久。
<tx:annotation-driven proxy-target-class="true" transaction-manager="txManager" />
加不加这个都一样,事物始终不回滚
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@127.0.0.1:1521:XE">
</property>
<property name="username" value="OA"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>./Admin.hbm.xml</value>
<value>./Dairy.hbm.xml</value></list>
</property></bean>
<bean id="AdminDAO" class="AdminDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="DairyDAO" class="DairyDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean></beans>
455 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Exception in thread "main" org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:622)
你们可以帮我解决下嘛
谢谢