Home  >  Q&A  >  body text

java - 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"/><!-- 注解驱动 -->

怪我咯怪我咯2713 days ago767

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-17 13:32:00

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

    The error is obvious, and then your comment question why opensession works?
    I recommend you to read 2 posts about using getCurrentSession() to obtain session in SSH.
    The difference between getCurrentSession and openSession(), although it was written a little earlier and is not the same as the configuration you used, the implementation of hibernate is the same. Of course, if you still don’t understand something, ask the person who gave you the textbook. .

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 13:32:00

    Don’t write private in the install method. It should be private that causes the transactional AOP proxy to not take effect (private cannot be overridden by inheritance).

    reply
    0
  • Cancelreply