2014-03-27 151 views
0

我有一個春季XA交易的工作項目(http://www.javaworld.com/article/2077714/java-web-development/xa-transactions-using-spring.html)。我修改了下載的項目並創建了pom.xml,因爲我必須將其與我的項目集成。最初我從春季3x開始。我爲春天3x正常工作。現在我必須將它與hibernate集成,所以爲了創建hibernate會話工廠,我聲明將它遷移到spring 4x。從3x春季遷移到4x創建休眠會話工廠

我已經按照我的配置文件條目春季3倍

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) 

是否有任何其他更改,我必須使這個項目與春季4x工作。

我想遷移到春天4倍,因爲我具備創建會話工廠如下:

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

回答

0

你混合Spring4和Hibernate4。如果您嘗試將您的應用程序遷移到Hibernate4,那麼您不需要對您的配置進行任何更改就可以將您的應用程序遷移到Spring 4.您的最新代碼段將會有效。

您還可以省略XML配置中的版本altogher,因爲在spring的jar中有映射確保始終使用最新的可用版本。

+0

這就是我所做的:修改pom.xml以使4.0.0.RELEASE彈簧細節保持原始彈簧cofig文件相同,即http://www.springframework.org/schema/tx http:// www。 springframework.org/schema/tx/spring-tx-3.0.xsd。我仍然收到相同的錯誤消息 –

+0

我看不到錯誤消息是如何與問題相關的。這基本上是說它不能連接到JNDI樹來查找你的數據源。 –

+0

只有當我更改我的pom.xml以使用sprig 4x jar時,我纔會遇到此問題。如果我使用彈簧3x罐,這個問題不存在。 –

0

這就是我所做的解決問題:我有我的原始程序工作3.0.7.RELEASE。我試圖遷移到spring4,因爲我想使用org.springframework.orm.hibernate4.LocalSessionFactoryBean。通過使用4.0.0.RELEASE遷移到spring4導致上述異常。

Spring 3.1在Spring Framework中添加了Hibernate 4支持。所以我用3.1.0.RELEASE替換了3.0.7.RELEASE,問題(異常)消失了。