繁体   English   中英

为什么openSession不起作用,但getCurrentSession在Spring Hibernate中工作

[英]Why doesn't openSession work but getCurrentSession works in Spring Hibernate

我已经编写了一个示例Spring Hibernate应用程序o了解Spring hibernate集成的工作原理。

这是我的applicationContext.xml

<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"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

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

    <tx:annotation-driven />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
        <property name="username" value="system" />
        <property name="password" value="admin_123" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean ">
        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan" value="com.general"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

然后,我的服务类就是这样

package com.general;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;


@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{

    @Autowired
    private SessionFactory sessionFactory;

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

    @Transactional
    public void saveEmployee(Employee emp) {
        Session session = sessionFactory.getCurrentSession();//.openSession();
        session.save(emp);
    }

}

而我的主要课程是

public class App {

    public static void main(String[] args) {
        System.out.println("load context");
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Employee em = new Employee();
        em.setId(1l);
        em.setName("John");

        EmployeeService emService = (EmployeeService) context.getBean("employeeService");
        emService.saveEmployee(em);
    }

}

如果我使用getCurrentSession方法运行此应用程序,那么它运行正常并且员工被保存到数据库中但是如果我使用openSession方法,则不会触发任何SQL查询,因此不会将任何内容保存到数据库中。

我不确定为什么会这样。 可能是我对getCurrentSession()和openSession()没有正确的理解。 请有人让我知道背后的原因。

事实上, @Service注释类中的方法上有@Transactional以及TransactionManager意味着整个事务生命周期将由Spring管理。

调用saveEmployee方法时,Spring将打开一个Session ,启动一个事务,执行代码,提交事务并关闭Session 它启动的Session绑定到当前线程,可通过getCurrentSession()

如果改为使用openSession() ,则打开一个完全不相关的 Session ,而不是由Spring的TransactionManager管理。 因此, 除非您自己执行,否则不会提交事务,也不会关闭Session

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM