带有JTA事务管理器(JBoss 7)的SPRING,JPA(EclipseLink)-不提交给数据库 [英] SPRING, JPA(EclipseLink) with JTA Transaction Manager(JBoss 7) - NOT Committing to Database

查看:134
本文介绍了带有JTA事务管理器(JBoss 7)的SPRING,JPA(EclipseLink)-不提交给数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个示例-带有JTA事务管理器(JBoss 7)的SPRING,JPA(EclipseLink持久性提供程序).我观察到数据库中的所有数据都已正确显示在UI中以进行读取操作.但是,当涉及到保存/更新或删除操作时,服务层不会将工作提交给数据库.没有异常被捕获(我也检查了控制台/日志,还调试了代码,在该代码中我可以看到entityManager.persist/remove毫无例外地被调用).

I have created an example - SPRING, JPA(EclipseLink persistence provider) with JTA Transaction Manager(JBoss 7). I have observed that all the data in database is being shown in UI properly for the read operations. But when it comes to save/update or delete operation the services layer is not committing the work to database. No exception is caught(I have checked the console/log too and also debugged the code where I can see entityManager.persist/remove is being invoked without any exception).

-代码清单- 1.在standalone.xml中配置数据源

--Code Listing-- 1. Datasource configuration in standalone.xml

<datasource jta="true" jndi-name="java:/mysql_customerdb3" pool-name="mysql_customerdb3_pool" enabled="true" use-java-context="true" use-ccm="true">
            <connection-url>jdbc:mysql://localhost:3306/customerdb</connection-url>
            <driver>mysql</driver>
            <security>
                <user-name>root</user-name>
                <password>root</password>
            </security>
            <statement>
                <prepared-statement-cache-size>10</prepared-statement-cache-size>
                <share-prepared-statements>true</share-prepared-statements>
            </statement>
        </datasource>
        <drivers>
            <driver name="mysql" module="com.mysql">
                <driver-class>com.mysql.jdbc.Driver</driver-class>
    <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
            </driver>
            <driver name="h2" module="com.h2database.h2">
                <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
            </driver>
        </drivers>

  1. module.xml中的数据库驱动程序配置

  1. Database driver configuration in module.xml

persistence.xml

persistence.xml

org.eclipse.persistence.jpa.PersistenceProvider java的:/mysql_customerdb3 com.springforbeginners.model.Customer

org.eclipse.persistence.jpa.PersistenceProvider java:/mysql_customerdb3 com.springforbeginners.model.Customer

customerdispatcher-servlet.xml

customerdispatcher-servlet.xml

<context:annotation-config />
<context:component-scan base-package="com.springforbeginners" />

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
    <property name="loadTimeWeaver" ref="loadTimeWeaver" />
    <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
</bean>

<bean id="loadTimeWeaver" class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" >
</bean>

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManagerName" value="java:jboss/TransactionManager"/>
    <property name="userTransactionName" value="java:jboss/UserTransaction"/>
</bean> 

<tx:annotation-driven transaction-manager="transactionManager" />

CustomerServiceImpl.java

CustomerServiceImpl.java

package com.springforbeginners.service;

package com.springforbeginners.service;

导入com.springforbeginners.dao.CustomerDAO; 导入com.springforbeginners.model.Customer; 导入java.util.List; 导入org.springframework.beans.factory.annotation.Autowired; 导入org.springframework.stereotype.Service; 导入org.springframework.transaction.annotation.Transactional;

import com.springforbeginners.dao.CustomerDAO; import com.springforbeginners.model.Customer; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;

@服务 公共类CustomerServiceImpl实现CustomerService { @Autowired 私人CustomerDAO customerDAO;

@Service public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerDAO customerDAO;

@Transactional
@Override
public void addCustomer(Customer customer) {
    customerDAO.addCustomer(customer);
}

@Transactional
@Override
public List<Customer> listCustomer() {
    return customerDAO.listCustomer();
}

@Transactional
@Override
public void removeCustomer(Integer customerId) {
    customerDAO.removeCustomer(customerId);
}

}

CustomerDAOImpl.java

CustomerDAOImpl.java

package com.springforbeginners.dao;

package com.springforbeginners.dao;

导入com.springforbeginners.model.Customer; 导入java.util.List; 导入javax.persistence.EntityManager; 导入javax.persistence.PersistenceContext; 导入org.springframework.stereotype.Repository;

import com.springforbeginners.model.Customer; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository;

@存储库 公共类CustomerDAOImpl实现CustomerDAO { @PersistenceContext(unitName ="CustomerDetailsPU3") 私有EntityManager实体管理器;

@Repository public class CustomerDAOImpl implements CustomerDAO { @PersistenceContext(unitName="CustomerDetailsPU3") private EntityManager entityManager;

@Override
public void addCustomer(Customer customer) {
    entityManager.persist(customer);
}

@Override
public List<Customer> listCustomer() {
    return entityManager.createQuery("select c from Customer c", Customer.class).getResultList();
}

@Override
public void removeCustomer(Integer customerId) {
    Customer customer = (Customer) entityManager.getReference(Customer.class, customerId);
    if (null != customer) {
        entityManager.remove(customer);
    }
}

}


我不知道丢失的东西和确切的位置.但是使用上述代码,读取操作将按预期方式工作.问题出在保存操作上.我已经将上面的示例转换为使用非JTA数据源(也已将standalone.xml修改为jta = false)并使用JpaTransactionManager,如下所示


I do not know what and where exactly is something missing. But with the above code the read operations are working as expected. Problem is with save operations. I have converted the above example to use non-JTA datasource(also modified standalone.xml for jta=false) and to use JpaTransactionManager as below

使用非JTA数据源和'org.springframework.orm.jpa.JpaTransactionManager',所有操作(读取以及保存/更新/删除)都可以正常工作.

With non-JTA datasource and 'org.springframework.orm.jpa.JpaTransactionManager' all operations(read as well as save/update/delete) are working fine.

但是我的示例的JTA版本无法正常工作(保存操作不将工作提交到数据库).任何帮助/指针都表示赞赏.

But the JTA version of my example is not working as expected(save operations not committing work to database). Any help/pointers appreciated.

谢谢 普拉卡什(Prakash)

Thanks Prakash

詹姆斯,

我将在JBoss上运行此应用程序.但是,JBoss上的一个数据源以及Glassfish和事务上的另一个数据源应同时跨越两个数据源上的保存操作.这就是我要实现的目标.我有一个Web应用程序,其中包括正在JBoss上运行的spring for service(数据)层.

I will be running this application on JBoss. But one datasource on JBoss and other on Glassfish and transaction should span save operation on both datasources simultaneously. This is what I am trying to achieve. I have a web application including spring for service(data) layer currently running on JBoss.

正如您之前所说的-我将有两个persistence.xmls,一个用于JBoss,一个用于Glassfish.当我第一次这样做时,我不确定事务(跨越不同服务器上的两个数据源,在本例中为JBoss和Glassfish)是否可以完全由JBoss执行(以防整个业务逻辑驻留在其中).部署在JBoss上的serviceImpl类)?在这种情况下,我将使用JBoss事务管理器(属性名="transactionManagerName" value ="java:jboss/TransactionManager").这足够了吗?或者我也需要同样具有Glassfish交易管理器吗?抱歉,这造成了混乱.

As you said earlier - I will have two persistence.xmls one for JBoss and one for Glassfish. As I am doing this first time I was/am in doubt whether the transaction(that spans two datasources on different servers-in this case JBoss and Glassfish), can this be executed entirely by JBoss(in case if the entire business logic resides in serviceImpl class deployed on JBoss)? In this case I will be using JBoss transaction manager( property name="transactionManagerName" value="java:jboss/TransactionManager" ). Is this sufficient or do I need to similarly have Glassfish transaction manager too? Sorry if this has created the confusion.

我的另一个问题是,是否有规定在persistence.xml/其他任何地方指定jndi端口?(当然,我将有两个不同的persistence.xmls,我将目标服务器称为JBoss,而在其中称为Glassfish.另一个).

Another question from me was that is there a provision for speifying jndi ports in persistence.xml/anywhere else?(Definitely I will have two different persistence.xmls and I will mention the target server as JBoss in one and as Glassfish in another).

我们在春季有没有一种技术可以通过这种技术将业务逻辑分布在JBoss/Glassfish之类的不同服务器之间,并且仍然可以在单个事务下进行?我不知道这是否可以选择.您是否在谈论这种情况,在这种情况下,每个服务器需要两个不同的部署脚本?

Do we have a technique in spring by which business logic can be distributed across different servers like JBoss/Glassfish and still under one single transatcion? I did not know if this can be an option. Were u talking about this scenario in which it will require two different deployment scripts one for each server?

谢谢 普拉卡什(Prakash)

Thanks Prakash

推荐答案

您的persistence.xml是什么?

What is your persistence.xml?

由于使用的是JTA,因此必须定义"eclipselink.target-server" ="JBoss"

Since you are using JTA, you must define the "eclipselink.target-server"="JBoss"

这篇关于带有JTA事务管理器(JBoss 7)的SPRING,JPA(EclipseLink)-不提交给数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆