Spring 交易没有开始

Spring transaction does not start

环境:Eclipse Keppler,jetty 7.5.1, Spring3.2.1、休眠 4.2.5、甲骨文 11

问题:休眠实体保存不适用于数据库物理

问题原因可能是:没有活跃的交易

问:为什么交易没有开始?

注意:如果我将 openSession() 更改为 getCurrentSession(),一切正常。交易开始 & 实体物理保存到数据库。


GenericDaoImpl:

@Transactional(propagation = Propagation.REQUIRED, readOnly = false, 
               value = "transactionManager")
public abstract class GenericDaoImpl<T, ID extends Serializable> implements GenericDao<T, ID> {

    private Class<T> persistentClass;   

    protected SessionFactory sessionFactory;

    @Override
    public T addEntity(T entity) {

        Session session = null;

        try {
            session = sessionFactory.openSession();

            session.save(entity);
        } catch (Exception e) {
            e.printStackTrace();    
        } finally {

            if(session != null){
                Transaction t = session.getTransaction();               
System.out.println("Transaction().isActive()......." + session.getTransaction().isActive());            
System.out.println("before: session.isOpen()  " + session.isOpen() + " trx wasCommitted " + t.wasCommitted());  

                session.close();

System.out.println("after: session.isOpen()  " + session.isOpen() + " trx  wasCommitted" + t.wasCommitted());               
        }
    }

    return entity;
}

UserDaoImpl 扩展 GenericDaoImpl:

@Component
@Scope("prototype")
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, value = "transactionManager")
public class UserDaoImpl extends GenericDaoImpl<User, Long> implements UserDao {
    .
    .
    }

执行以下代码后调用:

userDao.addEntity(user);   

日志打印如下:

Transaction().isActive().......false
before: session.isOpen()  true trx wasCommitted false
after: session.isOpen()  false trx  wasCommittedfalse

交易记录:

[2016-07-15 10:42:04,567][DEBUG] Adding transactional method 'addEntity' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; 'transactionManager' 

databaseContext.xml:

<bean class="com.blabla.dao.local.implementations.UserDaoImpl"
    scope="prototype" name="userDao">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven />

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

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan"
        value="com.blabla.model.local,com.blabla.model.authorization, com.blabla.model.authentication" />
    <property name="entityInterceptor">
        <bean class="com.blabla.listeners.EntityInterceptor" />
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.transaction.flush_before_completion">true</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
            <prop key="hibernate.connection.driver_class">${jdbc.driver}</prop>
            <prop key="hibernate.connection.url">${jdbc.url}</prop>
            <prop key="hibernate.connection.username">${jdbc.user}</prop>
            <prop key="hibernate.connection.password">${jdbc.password}</prop>
        </props>
    </property>
</bean>
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.user}" />
    <property name="password" value="${jdbc.password}" />
    <property name="initialSize" value="5" />
    <property name="maxActive" value="10" />
</bean>

在您的示例中,很明显 spring 处理休眠会话和事务:

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

所以你不能使用这个:

session = sessionFactory.openSession();

因为您正在打开新的休眠会话并且spring对此一无所知。

使用:sessionFactory.getCurrentSession() 意味着 spring 将通过您的正确配置处理幕后的一切。