spring从3x迁移到4x以创建hibernate会话工厂

时间:2014-03-27 09:29:43

标签: spring transactions xa

我有一个Spring XA事务的工作项目(http://www.javaworld.com/article/2077714/java-web-development/xa-transactions-using-spring.html)。我修改了下载的项目并创建了pom.xml,因为我必须将它与我的项目集成。最初我从春季3x开始。我让它在春天3x正常工作。现在我必须将它与hibernate集成,所以为了创建hibernate会话工厂,我说它将它迁移到spring 4x。

我在spring 3x的配置文件中有以下条目

spring配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <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-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

    <bean id="dsProps" class="java.util.Properties">
        <constructor-arg>
            <props>
                <prop key="user">root</prop>
                <prop key="password"></prop>
                <prop key="DYNAMIC_CLASS">com.findonnet.service.transaction.jboss.jdbc.Mysql
                </prop>
            </props>
        </constructor-arg>
    </bean>

    <bean id="dataSource1"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.arjuna.ats.jdbc.TransactionalDriver</value>
        </property>
        <property name="url" value="jdbc:arjuna:mysql://localhost:8888/mydb1" />
        <property name="connectionProperties">
            <ref bean="dsProps" />
        </property>
    </bean>


    <bean id="dataSource2"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName">
            <value>com.arjuna.ats.jdbc.TransactionalDriver</value>
        </property>
        <property name="url" value="jdbc:arjuna:mysql://localhost:8888/mydb2" />
        <property name="connectionProperties">
            <ref bean="dsProps" />
        </property>
    </bean>


    <!-- ===================================================== -->
    <!-- ==== TRANSACTION MANAGER CONFIG ===================== -->
    <!-- ===================================================== -->

    <bean id="jbossTransactionManager"
        class="com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple">
    </bean>

    <bean id="jbossUserTransaction"
        class="com.arjuna.ats.internal.jta.transaction.arjunacore.UserTransactionImple" />

    <!-- use the JtaTransactionManager, since we have multiple resources to 
        deal with -->
    <bean id="transactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager">
            <ref bean="jbossTransactionManager" />
        </property>
        <property name="userTransaction">
            <ref bean="jbossUserTransaction" />
        </property>
    </bean>


    <!-- Begin sequenceDAO bean. Handles persistence of seq num in the DB -->
    <bean id="sequenceDAO" class="com.findonnet.persistence.MessageSequenceDAO">
        <property name="dataSource">
            <ref bean="dataSource1" />
        </property>
    </bean>
    <!-- End sequenceDAO bean -->

    <!-- Begin sequenceDAO2 bean. Handles persistence of seq num in the DB -->
    <bean id="sequenceDAO2" class="com.findonnet.persistence.MessageSequenceDAO">
        <property name="dataSource">
            <ref bean="dataSource2" />
        </property>
    </bean>
    <!-- End sequenceDAO2 bean -->


    <bean id="eventHandlerTarget" class="com.findonnet.messaging.EventHandler">
    </bean>

    <!-- declarative transaction demarcation -->
    <bean id="eventHandler"
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="target">
            <ref bean="eventHandlerTarget" />
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="handle*">PROPAGATION_REQUIRED,-RuntimeException</prop>
            </props>
        </property>
    </bean>     
    </beans>

并且pom.xml有以下条目:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>3.0.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.0.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.7.RELEASE</version>
    </dependency>

这完全没问题。现在,如果我想迁移到spring4x,我会做出以下更改:

    <?xml version="1.0" encoding="UTF-8"?>
    <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 ">

并按如下方式更新我的pom.xml:

            <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>

当我进行上述更改并运行我的程序时,我收到以下错误:

    org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:628)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:907)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:968)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:973)
        at com.findonnet.persistence.MessageSequenceDAO.insertSequence(MessageSequenceDAO.java:22)
        at com.findonnet.messaging.EventHandler.handleEvent(EventHandler.java:53)
        at com.findonnet.messaging.EventHandler$$FastClassByCGLIB$$dd9a9fb0.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:713)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646)
        at com.findonnet.messaging.EventHandler$$EnhancerByCGLIB$$14d5f3ff.handleEvent(<generated>)
        at com.findonnet.messaging.MainApp.main(MainApp.java:92)

我是否需要进行任何其他更改才能使此项目与spring 4x一起使用。

我正在尝试迁移到spring 4x,因为我必须按如下方式创建会话工厂:

<bean id="sessionFactoryPayment"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSourcePayment" />
        <property name="configLocation">
            <value>classpath:payment.hibernate.cfg.xml</value>
        </property>
    </bean>

2 个答案:

答案 0 :(得分:0)

你正在混合Spring4和Hibernate4。您无需对配置进行任何更改即可将应用程序迁移到Spring 4.如果您尝试将应用程序迁移到Hibernate4,则最新的代码段将无效。

您还可以在XML配置中省略版本altogher,因为spring的jar中有一个映射,可确保始终使用最新的可用版本。

答案 1 :(得分:0)

这就是我为解决问题所做的工作:我的原始程序适用于3.0.7.RELEASE。我试图迁移到spring4,因为我想使用org.springframework.orm.hibernate4.LocalSessionFactoryBean。通过使用4.0.0.RELEASE迁移到spring4导致上述异常。

3.1中的Spring Framework添加了Hibernate 4支持。所以我用3.1.0.RELEASE替换了3.0.7.RELEASE,问题(异常)消失了。