Spring事务没有启动

时间:2016-07-15 07:52:43

标签: spring transactions spring-transactions

环境:Eclipse Keppler,码头7.5.1,              Spring 3.2.1,Hibernate 4.2.5,Oracle 11

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

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

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

注意:如果我将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>

1 个答案:

答案 0 :(得分:0)

在您的示例中,spring处理hibernate会话和事务非常明显:

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

所以你不能用这个:

session = sessionFactory.openSession();

因为你正在打开新的hibernate会话而且spring对此一无所知。

使用:sessionFactory.getCurrentSession()表示spring将使用正确的配置处理场景背后的所有内容。