本文介绍了Mule JPA持久未插入或更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mule JPA模块来检索和插入/更新数据.我可以检索数据,但不能更新数据库.它没有给出任何错误,并且根据日志,似乎记录已更新.但是:(如果我检查数据库,则不会插入任何记录.我相信交易没有得到落实.

I am using Mule JPA module for retrieving and insert/updating data. I can able retrieve data but not able to update the DB. Its giving no errors and as per the log it seems that the record got updated. But :( If I check DB no recorded is getting inserted. I believe that the transaction is not getting committed.

请帮助我如何解决此问题.

Please help me how to achieve this issue.

这是我的mflow

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jpa="http://www.mulesoft.org/schema/mule/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/jpa http://www.mulesoft.org/schema/mule/jpa/current/mule-jpa.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">

  <spring:beans>
           <spring:import resource="classpath:applicationContext.xml" />
    </spring:beans>


        <jpa:config name="Java_Persistence_API" entityManagerFactory-ref="entityManagerFactory" doc:name="Java Persistence API"/>
    <flow name="jpa-exampleFlow1" doc:name="jpa-exampleFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <logger message="payload ----&gt;&gt;&gt;&gt; #[payload]" level="INFO" doc:name="Logger"/>
     <logger message="#[message.inboundProperties.get(&quot;http.relative.path&quot;)]" level="INFO" doc:name="Logger"/>
        <choice doc:name="Choice">
            <when expression="#[message.inboundProperties.get(&quot;http.relative.path&quot;)==&quot;getContact&quot;]">
                <logger message="in GetContact" level="INFO" doc:name="Logger"/>
                <component class="com.test.test.GetContact" doc:name="Java"/>
                <jpa:query statement="from ContactEO contact where contact.firstName = :firstName" queryParameters-ref="#[payload:]"></jpa:query>
                    <!-- <jpa:query statement="from ContactEO contact where contact.id = :id" queryParameters-ref="#[payload:]"/> -->
            </when>
            <when expression="#[message.inboundProperties.get(&quot;http.relative.path&quot;)==&quot;addContact&quot;]">
                <logger message="In AddContact" level="INFO" doc:name="Logger"/>
                  <component class="com.test.test.AddContact" doc:name="Java"/>
               <transactional action="ALWAYS_BEGIN" doc:name="Transactional">
                 <jpa:persist  entity-ref="#[payload:]" config-ref="Java_Persistence_API" />
               </transactional>
            </when>
        </choice>
         <json:object-to-json-transformer doc:name="Object to JSON"/>
        <logger message="payload ----&gt;&gt;&gt;&gt; #[payload]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

这是我的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
        version="1.0">

    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">

    </persistence-unit>

</persistence>

这是我的控制台日志

2014-05-25 23:41:38,533 INFO [org.mule.api.processor.LoggerMessageProcessor] - <payload ---->>>> /addContact>
2014-05-25 23:41:38,568 INFO [org.mule.api.processor.LoggerMessageProcessor] - <addContact>
2014-05-25 23:41:38,586 INFO [org.mule.api.processor.LoggerMessageProcessor] - <In AddContact>
Hibernate: insert into contact (EMAIL, FIRSTNAME, LASTNAME) values (?, ?, ?)
2014-05-25 23:41:38,811 INFO [org.mule.api.processor.LoggerMessageProcessor] - <payload ---->>>> {"firstName":"king","lastName":"verma","email":"[email protected]","id":9}>

推荐答案

最后,通过创建启动事务的CustomTransactionFactory,可以使用MULE JPA传输来提交事务.

Finally could able to commit transaction using MULE JPA transport, by creating CustomTransactionFactory which starts the transaction.

public class CustomTransactionFactory implements TransactionFactory{

@Override
public Transaction beginTransaction(MuleContext muleContext)
        throws TransactionException {
    EntityManagerFactory emf = muleContext.getRegistry().lookupObject("entityManagerFactory");
    TransactionFactory tf = new JPATransactionFactory();

    Transaction tx = tf.beginTransaction(muleContext);
    tx.bindResource(emf, emf.createEntityManager());
    tx.begin();
    return tx;
}

@Override
public boolean isTransacted() {
    return true;
}

}

通过如下所述在入站端点中引用自定义事务管理器,我们可以实现流级别的事务.

By referring custom transaction manager in In-bound endpoint as below, we can achieve flow level transactions.

 <flow name="jpa_exampleFlow1" doc:name="jpa_exampleFlow1">
    <http:inbound-endpoint exchange-pattern="request-response"   doc:name="HTTP" address="http://localhost:9090/jpa_example">
    <custom-transaction action="ALWAYS_BEGIN" factory-ref="transctionManager"/>

注意:不再需要交易阻止.

Note: Transactional blocks are no more required.

这篇关于Mule JPA持久未插入或更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 11:41