Camel + JBoss + JPA: TransactionRequiredException: Unable to register for JTA transaction despite JTA data source - jpa

I have an application based on Apache Camel that runs on JBoss AS 7.1 application server.
In one Camel route, I let Camel read XML files, unmarshal them to JPA entity objects using JAXB, and write them to a database using the Camel jpa:// component.
However, whenever an entity is sent to a JPA endpoint, Camel gives me the following TransactionRequiredException:
javax.persistence.TransactionRequiredException: joinTransaction has been called on a resource-local EntityManager which is unable to register for a JTA transaction.
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.registerIfRequired(EntityTransactionWrapper.java:91)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.joinTransaction(EntityManagerImpl.java:2092)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.doJoinTransaction(ExtendedEntityManagerCreator.java:360)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:327)
at com.sun.proxy.$Proxy48.joinTransaction(Unknown Source)
at org.apache.camel.component.jpa.JpaProducer$1.doInTransaction(JpaProducer.java:64)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:131)
at org.apache.camel.component.jpa.JpaProducer.process(JpaProducer.java:61)
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
at org.apache.camel.processor.SendProcessor$2.doInAsyncProducer(SendProcessor.java:152)
at org.apache.camel.impl.ProducerCache.doInAsyncProducer(ProducerCache.java:304)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:147)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:424)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:51)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:120)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:424)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:105)
at org.apache.camel.processor.MulticastProcessor.doProcessParallel(MulticastProcessor.java:735)
at org.apache.camel.processor.MulticastProcessor.access$200(MulticastProcessor.java:82)
at org.apache.camel.processor.MulticastProcessor$1.call(MulticastProcessor.java:303)
at org.apache.camel.processor.MulticastProcessor$1.call(MulticastProcessor.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
What I don't understand is that JPA insists that my EntityManager is resource-local. In my persistence.xml, I've defined the data source as JTA:
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="foobar">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/myDS</jta-data-source>
<mapping-file>META-INF/eclipselink-orm.xml</mapping-file>
<!-- ... lots of entity classes omitted ... -->
<properties>
<property name="eclipselink.weaving" value="static"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
The relevant section in my applicationContext.xml is this:
<!-- snip -->
<import resource="classpath:/datasource.xml"/>
<tx:annotation-driven />
<bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="transactionManager" ref="transactionManager"/>
</bean>
<camelContext xmlns="http://camel.apache.org/schema/spring" id="foobar-context" useMDCLogging="true">
<!-- ... most routes omitted ... -->
<camel:route id="readXML">
<camel:from uri="file://C:/Data/xmldir"/>
<camel:to uri="direct:persistProduct"/>
</camel:route>
<camel:route id="persistProduct">
<camel:from uri="direct:persistProduct"/>
<camel:unmarshal ref="tnxJAXB"/>
<camel:to uri="jpa:com.foo.bar.Product"/>
</camel:route>
<!-- ... a lot of other unrelated stuff omitted ... -->
</camelContext>
<!-- snip -->
For testability, the data source stuff is in another file:
datasource.xml (complete):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
">
<jee:jndi-lookup id="myDS" jndi-name="java:jboss/datasources/myDS" />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="foobar"/>
</bean>
<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
If my research online was thorough, then all this is supposed to be correct and transaction management should work.
However, it doesn't.
What am I doing wrong?
The package versions I'm using:
Camel 2.14.3
Spring 4.1.6.RELEASE
Eclipselink 2.6.0-M3
JBoss AS 7.1.1-Final
Thanks in advance to everyone!

This exception usually occurs when you do not start a transaction. Remember transaction needs to be started at service level not at repository level. As service and repository code is not available here cannot help more.

Related

Error No Script Specified when using JPA Persistence Schema Generation

Running on Wildly 8.2.0.Final. I get the following error when trying to generate a database schema from a script.
Caused by: javax.persistence.PersistenceException: Schema generation configuration indicated to include CREATE scripts, but no script was specified
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="com.mycompany_mavenFlowChartDB5_war_1.0-SNAPSHOTPU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/postgresql</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name=""/>
<property name="javax.persistence.schema-generation.create-source" value="script"/>
<property name="javax.persistence.schema-generation.drop-source" value="script"/>
<property name="javax.persistence.schema-generation.scripts.action" value="none"/>
<property name="javax.persistence.schema-generation.scripts.drop-target" value="META-INF/drop-script.sql"/>
<property name="javax.persistence.schema-generation.scripts.create-target" value="META-INF/create-script.sql"/>
</properties>
</persistence-unit>
</persistence>
Where the create script is located:
/src/main/resources/META-INF/create-script.sql
Thanks in advance!
The Wildfly persistence schema generator gui creates incorrect property names for the drop and create targets.
The correct property names are
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop-script.sql"/>
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create-script.sql"/>
change create-script to create-script-source and drop-script to drop-script-source
source: https://github.com/hantsy/ee7-sandbox

How to create a bean which extends JpaRepository

I want to write a unit test for a DAO. But in our project, we have more than 200 DAO and 200 model classes. And we use jpaRepository to write DAO. But I have a problem when I run my unit test. I take a lot of time to create application context. I have an idea. I only create a bean DAO which I want to test instead of creating all DAO. But DAO of jpa:repository is interface, so I can not create a bean with class is interface. If creating a bean which extend jpa:reponsitories is imposible, can you suggest me other ways to run unit faster.
my unit test:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:META-INF/spring/testContext.xml"})
#TransactionConfiguration(defaultRollback = true)
// Importance, as the transaction will be rollback for each test
// give us a clean state.
#Transactional
public class CommunicationDetailDAOTest
{
#Autowired
CommunicationDetailDAO communicationDetailDAO;
#Test
public void testSomeThing()
{
System.out.println("helloworld");
}
}
file testContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:META-INF/spring/properties/prism_local_persistence_test.properties"/>
</bean>
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="/META-INF/persistence.xml"/>
<property name="persistenceUnitName" value="mobilePersistenceUnit"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}"/>
<!--using postgres driver-->
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource"></property>
<property name="entityManagerFactory" ref="entityManager"/>
</bean>
<!-- Spring Data Jpa setup -->
<jpa:repositories
base-package="com.discorp.model.dao"
entity-manager-factory-ref="entityManager"
transaction-manager-ref="transactionManager"></jpa:repositories>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--<context:component-scan base-package="com.qsoft"/>-->
</beans>
and file persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="mobilePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.discorp.model.common.Address</class>
<class>com.discorp.model.common.Application</class>
.... more than 200 model classes
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.show_sql" value="false"/>
<!--<property name="hibernate." value="false"/> -->
</properties>
</persistence-unit>
</persistence>
Make different config files for different tests an include only the dao(s) needed by the test:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:repository="http://www.springframework.org/schema/data/repository"
...
xsi:schemaLocation="http://www.springframework.org/schema/beans
...
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository.xsd
...">
...
<jpa:repositories base-package="path.to.your" >
<repository:include-filter type="assignable" expression="path.to.your.Repository"/>
</jpa:repositories>

JPA- No Persistence provider for EntityManager for MSAccess DB

Im trying to use JPA to connect to a MS access DB using a JDBC:ODBC connection. Here is my Persistance.xml code :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
<persistence-unit name="DSRJPA">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>com.entity.AccessEntity</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:odbc:MSAccessDB"/>
<property name="toplink.jdbc.user" value="admin"/>
<property name="toplink.jdbc.driver" value="sun.jdbc.odbc.JdbcOdbcDriver"/>
<property name="toplink.jdbc.password" value="admin"/>
<property name="toplink.jdbc.read-connections.min" value="1"/>
<property name="toplink.jdbc.write-connections.min" value="1"/>
</properties>
</persistence-unit>
</persistence>
But when i run the code, i get this error :
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named DSRJPA: The following providers:
oracle.toplink.essentials.PersistenceProvider
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
Returned null to createEntityManagerFactory.
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at com.test.Access.main(Access.java:19)
Please throw me some light on this error.
Thanks in Advance.
Have you referenced your persistence unit in the class?
i.e.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("DSRJPA");

Conflicting persistence unit definitions

Spring 3.1.1
Maven 3.0.4
JPA
Eclipse wtp indigo
Hello,
I am getting the following exception on server startup. I have just one persistence.xml in my project. Any ideas? Thanks Eric
Caused by: java.lang.IllegalStateException: Conflicting persistence unit definitions for name 'cassandra_pu': file:/C:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/cassandra/WEB-INF/classes/, file:/C:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/cassandra/WEB-INF/classes/
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:362)
at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:326)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:235)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
... 62 more
My context file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="hamacher.cassandra.controller" />
<context:component-scan base-package="hamacher.cassandra.dao" />
<context:component-scan base-package="hamacher.cassandra.service" />
<context:annotation-config />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="hamacher.cassandra.entity"/>
<property name="persistenceUnitName" value="cassandra_pu"/>
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
My persistence.xml file is in META-INF where it should be:
<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="cassandra_pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<properties>
<property name="kundera.nodes" value="localhost"/>
<property name="kundera.port" value="9160"/>
<property name="kundera.keyspace" value="KunderaExamples"/>
<property name="kundera.dialect" value="cassandra"/>
<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
<property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider"/>
<property name="kundera.cache.config.resource" value="/ehcache-test.xml"/>
</properties>
</persistence-unit>
</persistence>
Try adding,
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="emf-p"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="persistenceXmlLocation" value="/WEB-INF/classes/META-INF/persistence.xml" /> -->
<property name="persistenceUnitManager" ref="pum"/>
<property name="persistenceUnitName" value="personnel_cass_pu" />
<property name="loadTimeWeaver">
<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
To define your persistence.xml location with Spring bean.
You can also refer:
https://github.com/xamry/twitample/blob/master/src/main/resources/appContext.xml
for persistence.xml specific configuration.

How do I make this spring class not found error go away?

Will someone please explain why I'm getting this build error, when the class is obviously there. Here's the error -
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.NoClassDefFoundError: org/springframework/aop/scope/ScopedProxyUtils
and here's my spring-servlet.xml file -
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan
base-package="com.website" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<value>/WEB-INF/messages/messages</value>
</property>
<property name="cacheSeconds" value="60" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>
I also have the aop jar on my classpath-
<classpathentry kind="lib" path="WebContent/WEB-INF/lib/org.springframework.aop-sources-3.0.5.RELEASE.jar"/>
That is the sources jar...
path="WebContent/WEB-INF/lib/org.springframework.aop-sources-3.0.5.RELEASE.jar"
Get the binary release version.