没有活动事务,doWork无效

时间:2014-08-25 14:34:53

标签: java spring hibernate db2

我们正在升级到Hibernate 4.如果你不关心这个设置,我们会得到一个该死的:

<prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop>

如果我们设置它,我们得到:

Caused by: org.hibernate.HibernateException: doWork is not valid without active transaction
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) ~[hibernate-core-4.3.0.Final.jar:4.3.0.Final]

如果我们将其遗漏,我们会得到:

org.hibernate.HibernateException: No CurrentSessionContext configured!
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1010)

我应该注意这个代码在没有设置的情况下在hibernate 3中工作。代码并没有真正改变。事实上,我们实际上已经在hibernate 4中工作了一段时间,直到最近从DB2 9.5升级到10.5。我不确定这会对hibernate配置问题产生什么影响,但这似乎是相关的主要变化。

更新 - doWork没有改变,但现在是:

        session.doWork(new Work() 
        {
            public void execute(Connection connection) throws SQLException 
            { 
                Properties props = new Properties();
                props.setProperty(WSConnection.CLIENT_ID, uId);
                props.setProperty(WSConnection.CLIENT_APPLICATION_NAME, appName);

                if (connection instanceof WSConnection) {
                    WSConnection wconn = (WSConnection) connection;
                    wconn.setClientInformation(props);
                } else {
                    log.error("Connection was NOT an instance of WSConnection so client ID and app could not be set");
                }
            }
        }

更新2 - Spring配置变得很长,但这里看起来似乎与交易有关。

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="gov.usdoj.afms" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
            <prop key="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</prop>
            <prop key="hibernate.bytecode.provider">javassist</prop>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.default_schema">K702PRDR</prop>
        </props>
    </property>
</bean>


<!--Scans the classpath of this application for @Components to deploy as beans -->
<tx:annotation-driven/>
<context:annotation-config />
<context:component-scan base-package="gov.usdoj.afms" />

<!--BEGIN Data sources - By convention data sources are prefixed with the
    schema name against which they operate. -->
<jee:jndi-lookup id="dataSource" jndi-name="${ds.jndi}" />
<bean id="transactionManager" class="gov.usdoj.afms.cats.util.hibernate.AfmsHibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

自定义txmanager只设置一些连接属性。我们在整个hibernate 3中都使用过它,最初它使用了4个。

更新3 - 我会说我们已经学会了#34;我们认为这可能是一个类加载器交易。在RAD 7中运行时,我们总是必须将jpa 2.1放在ext文件夹中才能加载它。在RAD 8.5中,我们计划使用parent_last类加载器(ext技巧不再起作用)。但这似乎抛出了上述例外情况。

如果我们使用类加载器设置从RAD 8.5部署,它可以工作。如果我们使用ext文件夹技巧从RAD 7部署,它可以工作。但这两种技巧都不适用于其他版本。所以我们有解决方法,但它们涉及不同工作区的不同设置,这是不理想的。

0 个答案:

没有答案
相关问题