Hibernate 4无法获取当前线程的事务同步会话

时间:2015-02-11 08:46:59

标签: spring hibernate spring-transactions

我知道这不是一个新问题。例如,可以在问题和回复中找到大量收件人here。但是,我没有设法解决我的问题。

我使用Hibernate 4 + Spring 4和PostgreSQL。 我为全班提供了DAO和DAOImpl标记 @Transactional

package org.app.server.dao.impl;

@Repository
@Transactional
public class UserDAOImpl implements UserDAO {

  @Autowired
  private SessionFactory sessionFactory;

  public User getUser(String login) {
    User user =
            (User) sessionFactory.getCurrentSession().createQuery("from User p where p.login=:login")
                    .setParameter("login", login).uniqueResult();
    return user;
  }

  @Override
  public void addUser(User user) {
    sessionFactory.getCurrentSession().save(user);
  }
}

用户是 @Entity

package org.app.model;

@Entity
@Table(name = "person")
public class User implements Serializable {
  private static final long serialVersionUID = 1L;

  public User() {
  }

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(columnDefinition = "serial")
  private Integer id;

  @NotNull
  private String login;

  @NotNull
  private String password;

  private String email;

 // getters and setters
}

现在我从表单中注册用户。控制器捕获数据:

package org.app.controller;

@Controller
@Scope("prototype")
public class MyController {

@Autowired
private UserOperations userOperations;

  @ModelAttribute("user")
  public User getUserObject() {
    return new User();
  }

  @RequestMapping(value="/newuser", method = RequestMethod.POST)
  public ModelAndView newUser(@ModelAttribute("user") User user, BindingResult result) {
    userOperations.signup(user);
    return new ModelAndView("redirect:/somepage");
  }
}

并将其发送到UserOperations @Component ,以检查此类登录是否已存在并插入新用户。显然,这必须在一个交易中完成:

package org.app.server;

@Component
@Scope("prototype")
public class UserOperations {

  @Autowired
  private UserDAO userDAO;

  @Transactional
  public void signup(User user) {
    if (userDAO.getUser(user.getLogin()) == null) {
      userDAO.addUser(user);
    }    
  }
}

所以,当我运行它时,我有以下内容:

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    at org.app.server.dao.impl.UserDAOImpl.getUser(UserDAOImpl.java:30)
    at org.app.server.UserOperations.signup(UserOperations.java:25)
    at org.app.controller.StartPageController.newUser(StartPageController.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

这是我的xml配置: 的的applicationContext.xml

<context:component-scan base-package="org.app.server" />
<context:annotation-config />

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.databaseurl}"
    p:username="${jdbc.username}" p:password="${jdbc.password}" />

<bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" depends-on="flyway">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
         <array>
              <value>org.app.server</value>
              <value>org.app.model</value>
         </array>
    </property> 
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>
            <prop key="hibernate.hbm2ddl.auto">${jdbc.hbm2ddl}</prop>
        </props>
    </property>
</bean>

springmvc-servlet.xml

<context:component-scan base-package="org.app.controller, org.app.server" />
<mvc:annotation-driven />
<mvc:default-servlet-handler />

另一个有趣的事情是,当我直接从Controller单独使用getUser和addUser(注册)时,它可以正常工作。

3 个答案:

答案 0 :(得分:0)

您必须在通话前打开会话

User user = (User)sessionFactory.getCurrentSession().createQuery("from User p where p.login=:login").setParameter("login", login).uniqueResult();

所以你必须这样做:

Session session = sessionFactory.openSession();
User user = (User) session.createQuery("from User p where p.login=:login").setParameter("login", login).uniqueResult();

答案 1 :(得分:0)

在 UserDao.getUser(String login) 方法之前添加 @transactional 注释应该可以解决这个问题。

答案 2 :(得分:0)

就我而言,在针对特定包的组件扫描之后没有 tx:annotation-driven。