Hi,

I'm trying to migrate an application working with Karaf 3.0.5 to Karaf 4.0.4.
I ran into a problem with JPA and transaction management so I have created a 
small maven project showing it.
This sample is available on GitHub : 
https://github.com/nicolas-dutertry/test-jpa

The class TestServiceImpl in test-jpa-service has an EntityManager annoted with 
@PersistenceContext(unitname = "test")
The class DeleteManager also has an EntityManager annoted with 
@PersistenceContext(unitname = "test")

The method TestServiceImpl.delete is not transactional and it calls the 
transactional method DeleteManager.delete several times :

public class TestServiceImpl implements TestService {
    @PersistenceContext(unitName = "test")
    private EntityManager entityManager;

    private DeleteManager deleteManager;

    ...

    @Override
    public void delete(String... names) {
        for (String name : names) {
            System.out.println("Deleting " + name);
            deleteManager.delete(name);
        }
    }
}

public class DeleteManager {
    @PersistenceContext(unitName = "test")
    private EntityManager entityManager;

    ...

    @Transactional
    public void delete(String lastName) {
        Query query = entityManager.createQuery(
            "delete from Person where lastName = :lastName");

        query.setParameter("lastName", lastName);
        query.executeUpdate();
    }
}

At runtime it raised a javax.persistence.TransactionRequiredException during 
the second call to DeleteManager.delete in the for loop.

To reproduce the problem, compile the sample project with maven:
> mvn clean install

Then, launch Karaf 4.0.4 and install the kar:
> kar:install mvn:com.dutertry.test.karaf.jpa/test-jpa-kar/1.0.0-SNAPSHOT/kar

Then, create new entries in database:
> person:create Smith 25
> person:create Green 23

Finally delete them:
> person:delete Smith Green

This will output :
Deleting Smith
Deleting Green
Error executing command: Executing an update/delete query

With the following log :
2016-02-05 14:52:32,753 | ERROR | nsole user karaf | ShellUtil                  
      | 44 - org.apache.karaf.shell.core - 4.0.4 | Exception caught while 
executing command
javax.persistence.TransactionRequiredException: Executing an update/delete query
                at 
org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)
                at 
com.dutertry.test.karaf.jpa.service.impl.DeleteManager.delete(DeleteManager.java:33)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native 
Method)[:1.7.0_67]
                at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_67]
                at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_67]
                at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_67]
                at 
org.apache.aries.proxy.impl.ProxyHandler$1.invoke(ProxyHandler.java:54)[21:org.apache.aries.proxy.impl:1.0.4]
                at 
org.apache.aries.proxy.impl.ProxyHandler.invoke(ProxyHandler.java:119)[21:org.apache.aries.proxy.impl:1.0.4]
                at 
com.dutertry.test.karaf.jpa.service.impl.$DeleteManager1431996488.delete(Unknown
 Source)[131:test-jpa-service:1.0.0.SNAPSHOT]
                at 
com.dutertry.test.karaf.jpa.service.impl.TestServiceImpl.delete(TestServiceImpl.java:53)[131:test-jpa-service:1.0.0.SNAPSHOT]
                at Proxy77c92452_69bd_43a7_b242_ba213735af8a.delete(Unknown 
Source)[:]
                at 
com.dutertry.test.karaf.jpa.cmd.DeletePersonCommand.execute(DeletePersonCommand.java:30)[128:test-jpa-cmd:1.0.0.SNAPSHOT]
                at 
org.apache.karaf.shell.impl.action.command.ActionCommand.execute(ActionCommand.java:83)[44:org.apache.karaf.shell.core:4.0.4]
                at 
org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:67)[44:org.apache.karaf.shell.core:4.0.4]
                at 
org.apache.karaf.shell.impl.console.osgi.secured.SecuredCommand.execute(SecuredCommand.java:87)[44:org.apache.karaf.shell.core:4.0.4]
                at 
org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480)[44:org.apache.karaf.shell.core:4.0.4]
                at 
org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406)[44:org.apache.karaf.shell.core:4.0.4]
                at 
org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)[44:org.apache.karaf.shell.core:4.0.4]
                at 
org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)[44:org.apache.karaf.shell.core:4.0.4]
                at 
org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)[44:org.apache.karaf.shell.core:4.0.4]
                at 
org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:94)[44:org.apache.karaf.shell.core:4.0.4]
                at 
org.apache.karaf.shell.impl.console.ConsoleSessionImpl.run(ConsoleSessionImpl.java:270)[44:org.apache.karaf.shell.core:4.0.4]
                at java.lang.Thread.run(Thread.java:745)[:1.7.0_67]

Regards,
--
Nicolas Dutertry
Sopra HR Software - http://www.soprahr.com/

Reply via email to