Jboss 7 - Spring - 使用Jboss TransactionManager

时间:2012-12-17 08:56:06

标签: spring hibernate jboss

使用:Jboss 7.1.1,Hibernate 3.6.9,Spring 3.0.1。

我尝试从Spring Transaction Manager切换:

     <bean id="transactionManager"  
              class="org.springframework.orm.jpa.JpaTransactionManager">
         <property name="entityManagerFactory" ref="entityManagerFactory" />
         <property name="dataSource" ref="dataSource" />
    </bean>

到Jboss交易经理:

 <!-- search for transaction managers -->
 <tx:jta-transaction-manager/>

使用 org.springframework.orm.jpa.JpaTransactionManager 一切正常但如果我删除“transactionManager”bean并添加“”我收到以下错误:

javax.persistence.TransactionRequiredException: no transaction is in progress

我在服务器日志中找到了以下内容:

08:40:58,382 INFO  [org.hibernate.transaction.TransactionFactoryFactory] (MSC service 
            thread 1-1) Using default transaction strategy (direct JDBC transactions)
08:40:58,383 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] (MSC 
            service thread 1-1) No TransactionManagerLookup configured (in JTA 
            environment, use of read-write or transactional second-level cache is not 
            recommended)

这意味着什么?我的应用程序现在是否使用 java:jboss / TransactionManager ?我是否必须在Jboss上配置一些东西?

applicationContext.xml片段:

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="xxx" />
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceXmlLocation" value="classpath*:META-  
             INF/persistence.xml" />
    <property name="jpaVendorAdapter">
        <bean          
             class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="databasePlatform" 
             value="org.hibernate.dialect.SQLServerDialect" />
            <property name="showSql" value="false" />
            <property name="generateDdl" value="false" />
        </bean>
    </property>
</bean>

<tx:jta-transaction-manager/>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName" value="${database.jndi.path}" />

persistence.xml片段:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"      
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         version="2.0"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
         http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="xxx" transaction-type="RESOURCE_LOCAL">
  <!-- a lot of <class>com.MyClass</class> -->
   <properties>
        <property name="jboss.as.jpa.managed" value="false"/>
        <property name="hibernate.dialect" 
        value="org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect"/>
    </properties>
</persistence-unit>

persitence.xml中可能“RESOURCE_LOCAL”错误了吗?

domain.xml中:

               <datasource jta="true" jndi-name="java:jboss/datasources/myName" pool-name="myName" use-ccm="true">
                    <connection-url>jdbc:sqlserver://10.100.0.91:1433;databaseName=db;user=myUser;password=myPassword</connection-url>
                    <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
                    <driver>mssql</driver>
                    <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                    <pool>
                        <min-pool-size>10</min-pool-size>
                        <max-pool-size>20</max-pool-size>
                        <prefill>true</prefill>
                    </pool>
                    <security>
                        <user-name>myName</user-name>
                        <password>myPassword</password>
                    </security>
                    <validation>
                        <validate-on-match>false</validate-on-match>
                        <background-validation>false</background-validation>
                    </validation>
                    <statement>
                        <share-prepared-statements>false</share-prepared-statements>
                    </statement>
                </datasource>

- EDIT-- 现在我改变了我的applicationContext.xml和persistence.xml!

applicationContext.xml片段:

    <jee:jndi-lookup id="my_unit" jndi-name="java:jboss/my_unit" />


<tx:jta-transaction-manager/>


<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName" value="${database.jndi.path}" />
</bean>

persistence.xml片段:

?xml version="1.0" encoding="UTF-8" standalone="no"?>

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd“&GT;     

     <jta-data-source>java:jboss/datasources/wtb</jta-data-source>

    <properties>
        <property name="hibernate.transaction.manager_lookup_class"
            value="org.hibernate.transaction.JBossTransactionManagerLookup" />

        <property name="hibernate.dialect"
            value="org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect" />
    </properties>

</persistence-unit>

现在我在部署时遇到以下异常:

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: my_unit] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:162)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.start(PersistenceUnitServiceImpl.java:85)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.2.GA.jar:1.0.2.GA]
    ... 3 more
Caused by: org.hibernate.HibernateException: Dialect class not found: org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect
    at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:76)
    at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:64)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:176)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:71)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2270)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2266)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1735)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:84)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904)
    ... 9 more
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect]
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:141)
    at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:73)
    ... 20 more
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl$1.findClass(ClassLoaderServiceImpl.java:99)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306) [rt.jar:1.6.0_32]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) [rt.jar:1.6.0_32]
    at    
 org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:138)
    ... 21 more  

1 个答案:

答案 0 :(得分:0)

您是否在applicationContext中使用它:

 <tx:annotation-driven />

在你的方法中:

 @Transaction
 public void store(Message message){ ...}