1

我正在使用 Mule JPA 模块来检索和插入/更新数据。我可以检索数据但无法更新数据库。它没有给出任何错误,并且根据日志,似乎记录已更新。但是:(如果我检查没有记录的数据库被插入。我相信事务没有被提交。

请帮助我如何解决这个问题。

这是我的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":"vermaS@xxx.com","id":9}>
4

2 回答 2

0

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

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

}

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

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

注意:不再需要事务块。

于 2014-11-29T17:16:03.640 回答
0

在这里,您的控制台显示:- Hibernate:插入联系人(EMAIL,FIRSTNAME,LASTNAME)值(?,?,?)并且您的有效负载是:- {“firstName”:“king”,“lastName”:“verma”, email":"vermaS@xxx.com","id":9} ... 这个 id:"9" 将被安置在哪里?您的查询有 3 个字段firstname、lastname 和 email ..其中您的有效负载包含firstname、lastname、email 和 id

于 2014-05-26T06:25:03.463 回答