Jboss 7 - Spring - 使用 Jboss TransactionManager

Posted

技术标签:

【中文标题】Jboss 7 - Spring - 使用 Jboss TransactionManager【英文标题】:Jboss 7 - Spring - using Jboss TransactionManager 【发布时间】:2012-12-04 08:41:17 【问题描述】:

使用: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 sn-p:

<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 sn-p:

<?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”可能有误?

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

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

applicationContext.xml sn-p:

    <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 sn-p:

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

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

     <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  

【问题讨论】:

为什么使用c3p0而不是默认的JBoss连接池? 当您在standalone.xml 中定义数据源时,您可以添加&lt;pool&gt; 元素参见DataSource+configuration 我使用集群,所以 domain.xml 也一样? 是的,domain.xml也一样 好的,我必须切换到JTA事务类型吗? 【参考方案1】:

你在你的applicationContext中使用这个吗:

<tx:annotation-driven />

在你的方法中:

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

【讨论】:

以上是关于Jboss 7 - Spring - 使用 Jboss TransactionManager的主要内容,如果未能解决你的问题,请参考以下文章

jboss 异常处理

Linux Jboss下常用命令

JBoss 数据库连接池

java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljava/lang/Class;Ljava/lang/S

Spring + Jboss7 @Transactional 不工作

JBoss AS 7.1.1 中的 Spring 应用程序:意外元素:bean