【问题标题】:spring migration from 3x to 4x to create hibernate session factory春季从 3x 迁移到 4x 以创建休眠会话工厂
【发布时间】:2014-03-27 09:29:43
【问题描述】:

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

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

弹簧配置文件:

    <?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>

【问题讨论】:

    标签: spring transactions xa


    【解决方案1】:

    您正在混合 Spring4 和 Hibernate4。您无需更改任何配置即可将应用程序迁移到 Spring 4。如果您尝试将应用程序迁移到 Hibernate4,您的最新代码 sn-p 将是有效的。

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

    【讨论】:

    • 这就是我所做的:修改 pom.xml 以具有 4.0.0.RELEASE 弹簧细节并保持原始弹簧配置文件相同,即springframework.org/schema/txspringframework.org/schema/tx/spring-tx-3.0.xsd。我仍然收到相同的错误消息
    • 我看不到错误消息与问题的关系。这基本上是说它无法连接到 JNDI 树来查找您的数据源。
    • 只有当我将 pom.xml 更改为使用 sprig 4x jars 时,我才会遇到这个问题。如果我使用 spring 3x jars,这个问题就不存在了。
    • 请附上相关日志。真的,只更换罐子不应该这样做。
    • 我已将我的代码放在 dropbox.com/s/734k6l6seji3uk6/XA-Txn-mvn.rar 。如果打开 rar,根文件夹包含 MainApp.log。还有一个包含应用程序所需的 3 个 jar 的 lib 文件夹。可以使用 pom.xml 下载其他 jar。如果您需要我方面的任何其他信息,请告诉我。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多