Sping管理hibernate时的事务问题(No Hibernate Session bound to thread)


报错: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:685)
at cn.itcast.oa.util.Installer.install(Installer.java:24)
at cn.itcast.oa.util.Installer.main(Installer.java:88)

报错代码片段:

Java代码:
@Component
public class Installer {


 @Resource
private SessionFactory sessionFactory;

@Transactional
private void install(){
    Session session = sessionFactory.getCurrentSession();
    //保存超级管理员账户
    User user = new User();
    user.setLoginName("admin");
    user.setName("超级管理员");
    user.setPassword(DigestUtils.md5Hex("admin"));

    session.save(user);

    //保存权限数据
    Privilege menu, menu1, menu2, menu3, menu4, menu5;

    //=====
    menu= new Privilege("系统管理", null, null);
    menu1= new Privilege("岗位管理", "/role_list", menu);
    menu2= new Privilege("部门管理", "/department_list", menu);
    menu3= new Privilege("用户管理", "/user_list", menu);       
    session.save(menu);
    session.save(menu1);
    session.save(menu2);
    session.save(menu3);

}

public static void main(String[] args) {
    ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    Installer installer =  (Installer) ac.getBean("installer");
    installer.install();
}

}

Spring配置文件:


 <!-- 导入外部的properties文件 -->
  <context:property-placeholder location="classpath:jdbc.properties"/>

  <!-- 配置SessionFactory -->
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
     <property name="dataSource">
         <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">

             <property name="jdbcUrl" value="${jdbcUrl}"></property>
             <property name="driverClass" value="${driverClass}"></property>
             <property name="user" value="${user}"></property>
             <property name="password" value="${password}"></property>

             <!-- 其他配置 -->
            <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
             <property value="3" name="initialPoolSize"/> 
            <!--连接池中保留的最小连接数。Default: 3 -->
             <property value="3" name="minPoolSize"/> 
            <!--连接池中保留的最大连接数。Default: 15 -->
             <property value="5" name="maxPoolSize"/> 
            <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
             <property value="3" name="acquireIncrement"/> 
            <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
             <property value="8" name="maxStatements"/> 
            <!--maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
             <property value="5" name="maxStatementsPerConnection"/> 
            <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
             <property value="1800" name="maxIdleTime"/>

         </bean>
     </property>
  </bean>
  <!-- 配置声明式的事务管理(采用注解的方式) -->
  <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>
  <tx:annotation-driven transaction-manager="txManager"/><!-- 注解驱动 -->

hibernate spring java

时泪型吉原 10 years, 3 months ago

install方法不要写private看看,应该是private导致transactional的AOP代理没生效(private无法被继承覆盖)。

真杯具那啥 answered 10 years, 3 months ago

报错:Exception in thread "main" org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

报错很明显了,然后你的评论问题为什么opensession可以?
推荐你2个帖子看一下 SSH中使用getCurrentSession()获得session
getCurrentSession 与 openSession() 的区别
,虽然写的早了点和你用的配置不太相同,但hibernate的实现是一样的。当然如果还是不太明白的话,那就问问给你教材的人吧。。

我爱吃基勒 answered 10 years, 3 months ago

Your Answer