2

I'm trying to write a Mule ESB application that reads an XML file into a domain object and then writes that object to a MySQL database using JPA.

I have figured out most of the needed config but I am running into a problem where the Mule JPA module (https://github.com/mulesoft/mule-module-jpa) doesn't seem to be committing the sql queries. I have set the Mule JPA component to flush the queries and I can see them being written to the MySQL general log but a COMMIT is never done, so the data isn't ever stored. It seems that the doCommit() method on the JPATransaction class in the module isn't ever called, I can't see any log output for it. However, the transaction does seem to be bound using the bindResource method of the same class. This can be seen in the logs.

Any idea what I'm doing wrong here?

The mule flow:

<spring:beans>
        <spring:bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
            <spring:property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <spring:property name="url" value="jdbc:mysql://localhost:3306/interoperability_layer?user=root&amp;password=***" />
            <spring:property name="defaultAutoCommit" value="true" />
        </spring:bean>

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

        <spring:bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />

        <spring:bean id="eclipseLinkVendor" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <spring:property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
            <spring:property name="generateDdl" value="false"/>
            <spring:property name="showSql" value="false"/> 
        </spring:bean> 

        <spring:bean id="entityManagerFactory" name="Bean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <spring:property name="dataSource" ref="dataSource" />
            <spring:property name="persistenceUnitName" value="org.jembi.openhim.jpa" />
            <spring:property name="jpaDialect" ref="jpaDialect" /> 
            <spring:property name="jpaVendorAdapter" ref="eclipseLinkVendor" />
            <spring:property name="packagesToScan" value="domain"/>
        </spring:bean>
    </spring:beans>
    <jpa:config name="Java_Persistence_API" entityManagerFactory-ref="entityManagerFactory" doc:name="Java Persistence API"/>
    <flow name="openhim-report-adapterFlow1" doc:name="openhim-report-adapterFlow1">
        <vm:inbound-endpoint exchange-pattern="request-response" path="processReport" doc:name="VM">
        </vm:inbound-endpoint>
        <component class="org.jembi.openhim.IndicatorReportXmlToObjectTransformer" doc:name="Store indicator reports"/>
        <logger message="Converted XML to Report object" level="INFO" doc:name="Logger"/>
        <transactional action="ALWAYS_BEGIN" doc:name="Transactional">
            <jpa:persist config-ref="Java_Persistence_API" entity-ref="#[payload:]" doc:name="Java Persistence API" flush="true"/>
        </transactional>
        <logger message="Saved Report object using JPA" level="INFO" doc:name="Logger"/>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy"><![CDATA[message.payload = "";]]></scripting:script>
        </scripting:component>
        <http:response-builder status="201" doc:name="HTTP Response Builder" contentType="application/xml"/>
        <logger message="Sending HTTP responce" level="INFO" doc:name="Logger"/>
    </flow>

The log output:

INFO  2013-09-02 16:17:21,601 [[openhim-report-adapter].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: Converted XML to Report object
DEBUG 2013-09-02 16:17:21,633 [[openhim-report-adapter].connector.http.mule.default.receiver.02] org.mule.module.jpa.adapters.JPAModuleProcessAdapter: Persisting: org.jembi.openhim.Report@1749c7ac
DEBUG 2013-09-02 16:17:21,634 [[openhim-report-adapter].connector.http.mule.default.receiver.02] org.mule.module.jpa.adapters.JPAModuleProcessAdapter: Executing JPA command with message: org.jembi.openhim.Report@1749c7ac, command: org.mule.module.jpa.command.Persist@6bf22341 and parameters: null
[EL Info]: 2013-09-02 16:17:21.663--ServerSession(1208128432)--EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507-3faac2b
[EL Info]: connection: 2013-09-02 16:17:21.964--ServerSession(1208128432)--file:/home/ryan/MuleStudio/workspace/.mule/apps/openhim-report-adapter/classes_org.jembi.openhim.jpa login successful
DEBUG 2013-09-02 16:17:22,030 [[openhim-report-adapter].connector.http.mule.default.receiver.02] org.mule.module.jpa.JPATransaction: Binding JPA transaction: 621071ed-13da-11e3-a4cd-0907619c09d6
DEBUG 2013-09-02 16:17:22,043 [[openhim-report-adapter].connector.http.mule.default.receiver.02] org.mule.module.jpa.JPATransaction: Binding org.eclipse.persistence.internal.jpa.EntityManagerImpl@2eabef26 to org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@19e1bfba
DEBUG 2013-09-02 16:17:22,044 [[openhim-report-adapter].connector.http.mule.default.receiver.02] org.mule.module.jpa.command.Persist: Persisting entity: org.jembi.openhim.Report@1749c7ac
INFO  2013-09-02 16:17:22,134 [[openhim-report-adapter].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: Saved Report object using JPA
INFO  2013-09-02 16:17:22,286 [[openhim-report-adapter].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: Sending HTTP responce
DEBUG 2013-09-02 16:17:22,289 [[openhim-report-adapter].connector.http.mule.default.receiver.02] com.mulesoft.mule.config.pool.MonitoredThreadPoolExecutor: Stats{totalTime=979.67, startTime=25637681310485, endTime=25638660979198}
DEBUG 2013-09-02 16:17:22,289 [[openhim-report-adapter].connector.http.mule.default.receiver.02] com.mulesoft.mule.config.pool.MonitoredThreadPoolExecutor: Finished Work: org.mule.execution.FlowProcessingPhase$1@7a670f8b. in Thread[[openhim-report-adapter].connector.http.mule.default.receiver.02,5,main]. Active tasks: 1 (2 threads in a pool)
DEBUG 2013-09-02 16:17:22,290 [[openhim-report-adapter].http.request.dispatch.8085.01] com.mulesoft.mule.config.pool.MonitoredThreadPoolExecutor: Stats{totalTime=1007.19, startTime=25637654077832, endTime=25638661269425}
DEBUG 2013-09-02 16:17:22,290 [[openhim-report-adapter].http.request.dispatch.8085.01] com.mulesoft.mule.config.pool.MonitoredThreadPoolExecutor: Finished org.mule.transport.http.HttpRequestDispatcherWork@b6edc37. in Thread[[openhim-report-adapter].http.request.dispatch.8085.01,5,main]. Active tasks: 0 (1 threads in a pool)

UPDATE

And here is my persistence.xml file:

<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_2_0.xsd"
    version="2.0">
    <persistence-unit name="org.jembi.openhim.jpa" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

        <class>org.jembi.openhim.Report</class>
        <class>org.jembi.openhim.Indicator</class>
        <class>org.jembi.openhim.DataElements</class>

        <properties>
            <property name="eclipselink.weaving" value="false"/>
        </properties>
    </persistence-unit>
</persistence>
Ryan Crichton
  • 219
  • 2
  • 9
  • I'm not familiar with Spring so I don't know the workflow--however, you hit the nail on the head, EntityManager.getTransaction().commit(); is not being called. This happens often on the EE side of things if the code expects things to be container managed when it isn't configured properly. How and when does Spring cause a commit? You need to look there for answers. The class `org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect` probably has a lot of useful information as well. In this case I'm guessing -> `EclipseLinkJpaDialect.JpaDialect.cleanupTransaction(obj)`. – Daniel B. Chapman Sep 02 '13 at 14:58
  • It also could be as simple as: ` ` – Daniel B. Chapman Sep 02 '13 at 14:59
  • @DanielChapman Thanks, I've tried RESOURCE_LOCAL with no success. It seems like the Mule module (mule uses spring) isn't committing the transaction when it should, but I don't understand why not. – Ryan Crichton Sep 03 '13 at 06:57
  • RESOURCE_LOCAL would mean that you're handling transactions yourself. My guess is it needs to be "container managed". `Dialect.cleanupTransaction(ObjecT)` (debug it) is where that call should be made. – Daniel B. Chapman Sep 03 '13 at 14:15
  • I have found the exact same problem with this module using hibernate as the JPA provider. Hopefully Mulesoft will get around to fixing this someday soon.http://stackoverflow.com/questions/20565583/usage-of-mule-module-jpa-1-2-0-with-hibernate-in-3-4-0-ce-how-to-define-entitym – mmeyer Dec 16 '13 at 19:53

1 Answers1

0

Others have reported a similar issue with mule-module-jpa, the module is not maintained. Also, is it a requirement to use eclipselink? Have a look at a working Mule-JPA example, this uses Hibernate instead. A description of the example may be found here.

HTH

Community
  • 1
  • 1
Gabriel Dimech
  • 699
  • 5
  • 10