第一个方法
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

解决方案 »

  1.   

    spring的配置文件:
    <?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> 
      

  2.   

    我晕了,仔细检查原来是少了<tx:annotation-driven transaction-manager="txManager"/>
    但是仍然有事物不能回滚的问题
    希望有朋友可以来帮忙解答下
    让第一个方法抛出运行时异常
    public void addLog(Log log) {
    Session session = sessionFactory.getCurrentSession(); 
    session.save(log); 
    throw new RuntimeException("运行异常");
    }
    再次运行测试,发现成功向数据库中添加数据,事务并没有回滚
    这个问题我也是研究了好久。
      

  3.   

    <!-- 支持 @Transactional 标记 -->   
    <tx:annotation-driven proxy-target-class="true" transaction-manager="txManager" />
      

  4.   

    proxy-target-class="true"表示事务加在类上而不是接口。
      

  5.   

    proxy-target-class="true"
    加不加这个都一样,事物始终不回滚
      

  6.   

    你先看看你的数据库支持不支持事务,mySql有个选项是管这个的。。不知道其他的数据库。lz自己查查资料吧
      

  7.   

    spring的配置文件:
    <?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>
      

  8.   

     出错如下:
    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)
    你们可以帮我解决下嘛
    谢谢
      

  9.   

    菜鸟支招,在service 中加@Transactional ,不加这个,也找不到事务,也会报这个错。
      

  10.   

    我这个在action里面用的不是注解用的是手动查找service类,但是在其他类里面都用的是注解,前面说的是没添加事物支持,报了楼主说的错误,我吧<tx:annotation-driven transaction-manager="txManager"/>and@Transacrional加上了但是又报错,直接不能够通过手动方式在action里面注入这个service类了
      

  11.   

    1 必须加入到事务中。2在"transactionAttributes"配置所有方法,缺一个方法都不行!检察试试看。