简体   繁体   English

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

[英]Spring 4+ Hibernate 4: Could not obtain transaction-synchronized Session for current thread

I am trying to do Spring 4 + Hibernate 4 configuration. 我正在尝试做Spring 4 + Hibernate 4的配置。 I'm facing the error: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread Could you please help to identify where I am wrong? 我遇到了错误: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread能否请您帮助确定我哪里错了? Below are the configurations. 以下是配置。

spring-database.xml: 弹簧database.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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-4.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- MySQL data source -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/personal" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>

    <!-- Hibernate session factory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>/orm/Song.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            </props>
        </property>
    </bean>

    <bean id="songDao" class="com.letsdo.impl.SongImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- MUST have transaction manager, using aop and aspects  -->
     <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

SongImpl.java: SongImpl.java:

package com.letsdo.impl;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.letsdo.dao.SongDao;

@Repository
@Transactional
public class SongImpl implements SongDao{

    @Autowired
    private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    public List<String> getAllAlbums(){
        List<String> allAlbums = new ArrayList<String>();
        Query query = getSessionFactory().getCurrentSession().createQuery("Select DISTINCT Album from song");
        allAlbums = query.list();
        return allAlbums;

    }
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
}

HomeController.java: HomeController.java:

package com.letsdo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.letsdo.dao.SongDao;
import com.letsdo.impl.SongImpl;

@Controller
public class HomeController {

    @Autowired 
    private SongImpl songDao;
    @RequestMapping(value="/", method=RequestMethod.GET)
    public ModelAndView welcomePageBeforeLogin(HttpServletRequest request, HttpServletResponse response,HttpSession session){
        ModelAndView model = new ModelAndView();
        List<String> album = songDao.getAllAlbums();
        model.addObject("albumsize",album.size());
        model.setViewName("hello");
        return model;
    }
}

mvc-dispatcher-servlet.xml: MVC-调度 - servlet.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.letsdo.*" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

</beans>

It looks like that you have not setup transactions properly in your configuration. 您似乎在配置中没有正确设置事务。 This thread resolve around similar issue. 线程解决了类似的问题。 Also check this . 还要检查一下

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring和Hibernate-无法获得当前线程的事务同步会话 - Spring and Hibernate - Could not obtain transaction-synchronized Session for current thread Hibernate无法获取当前线程的事务同步会话 - Hibernate Could not obtain transaction-synchronized Session for current thread Hibernate SessionFactory,无法获取当前线程的事务同步会话 - Hibernate SessionFactory and could not obtain transaction-synchronized session for current thread 休眠5-无法获取当前线程的事务同步会话; - hibernate 5 - Could not obtain transaction-synchronized Session for current thread; 休眠-无法获取当前线程的事务同步会话 - Hibernate - Could not obtain transaction-synchronized Session for current thread 迁移到Hibernate 4 + Spring 4.2.2:org.hibernate.HibernateException:无法获取当前线程的事务同步Session - migration to hibernate 4 + spring 4.2.2: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final-无法获得当前线程的事务同步会话 - Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final - Could not obtain transaction-synchronized Session for current thread 无法获取当前线程的事务同步会话-Hibernate-Spring - Could not obtain transaction-synchronized Session for current thread - Hibernate-Spring Spring MVC和Hibernate无法为当前线程获取事务同步的Session - Spring MVC & Hibernate Could not obtain transaction-synchronized Session for current thread Spring 4和Hibernate 5集成:无法获取当前线程的事务同步会话 - spring 4 and hibernate 5 integration: could not obtain transaction-synchronized Session for current thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM