Updating entities with Aries JPA in Declarative Services

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Updating entities with Aries JPA in Declarative Services

Jens Offenbach
Hallo,
I have got a question regarding updates of JPA entities using Aries JPA with Declarative Services (DS). Hopefully, someone can help me.

Let's assume we have the Task entity from the example here: https://github.com/apache/aries/blob/trunk/jpa/examples/tasklist-model/src/main/java/org/apache/aries/jpa/example/tasklist/model/Task.java

When e.g. the fields "title" and "description" must be updated, is it mandatory to do it via JpaTemplate within a transaction like this:

jpa.tx(em -> {
    task.setDescription("New description");
    task.setTitle("New title");
    em.flush();
 });

or can safely be done without any transaction like this:

task.setDescription("New description");
task.setTitle("New title");

I would say, it can be done directly without any transactions on application-level because transaction-type is "JTA" and not "RESOURCE_LOCAL".

persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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="treedb" transaction-type="JTA">

                <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
                <!-- Only used when transaction-type=JTA -->
                <jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/treedb)</jta-data-source>
                <!-- Only used when transaction-type=RESOURCE_LOCAL -->
                <non-jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/treedb)</non-jta-data-source>

                <class>...</class>
                <exclude-unlisted-classes>true</exclude-unlisted-classes>

                <properties>
                        <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.H2Platform" />

                        <property name="eclipselink.weaving" value = "static"/>
                        <property name="eclipselink.weaving.internal" value="true"/>
                        <property name="eclipselink.weaving.lazy" value="true" />
                        <property name="eclipselink.weaving.changetracking" value="true" />
                        <property name="eclipselink.weaving.fetchgroups" value="true" />
                        <property name="eclipselink.weaving.eager" value="false" />

                        <property name="eclipselink.ddl-generation" value="create-tables" />
                        <property name="eclipselink.ddl-generation.output-mode" value="database" />
                        <property name="eclipselink.logging.level" value="FINEST" />
                </properties>
        </persistence-unit>
</persistence>

Thank you very much.

Regards,
Jens
Reply | Threaded
Open this post in threaded view
|

Re: Updating entities with Aries JPA in Declarative Services

Christian Schneider
Transaction type JTA means that you need  a JTA transaction instead of a
local transaction on the em.
In both cases it is necessary to be in a transaction when you do changes
to the database.

The JPATemplate allows to transparently begin and commit the
transaction. It also demarcates the lifecycle of the em. So outside the
outermost JPATemplate execution the em is closed and your entities are
detached.

Your code without a transaction might work but rather as a side effect
of persistence manager caching. For example hibernate does not directly
write to the database when you change properties.
So simply changing the properties might work outside the transaction.
Still I would not recommend it as you never know when hibernate decides
to write to the db.

Christian

On 18.04.2017 08:59, Jens Offenbach wrote:

> Hallo,
> I have got a question regarding updates of JPA entities using Aries JPA with Declarative Services (DS). Hopefully, someone can help me.
>
> Let's assume we have the Task entity from the example here: https://github.com/apache/aries/blob/trunk/jpa/examples/tasklist-model/src/main/java/org/apache/aries/jpa/example/tasklist/model/Task.java
>
> When e.g. the fields "title" and "description" must be updated, is it mandatory to do it via JpaTemplate within a transaction like this:
>
> jpa.tx(em -> {
>      task.setDescription("New description");
>      task.setTitle("New title");
>      em.flush();
>   });
>
> or can safely be done without any transaction like this:
>
> task.setDescription("New description");
> task.setTitle("New title");
>
> I would say, it can be done directly without any transactions on application-level because transaction-type is "JTA" and not "RESOURCE_LOCAL".
>
> persistence.xml:
> <?xml version="1.0" encoding="UTF-8"?>
> <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="treedb" transaction-type="JTA">
>
> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
> <!-- Only used when transaction-type=JTA -->
> <jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/treedb)</jta-data-source>
> <!-- Only used when transaction-type=RESOURCE_LOCAL -->
> <non-jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/treedb)</non-jta-data-source>
>
> <class>...</class>
> <exclude-unlisted-classes>true</exclude-unlisted-classes>
>
> <properties>
> <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.H2Platform" />
>
> <property name="eclipselink.weaving" value = "static"/>
> <property name="eclipselink.weaving.internal" value="true"/>
> <property name="eclipselink.weaving.lazy" value="true" />
> <property name="eclipselink.weaving.changetracking" value="true" />
> <property name="eclipselink.weaving.fetchgroups" value="true" />
> <property name="eclipselink.weaving.eager" value="false" />
>
> <property name="eclipselink.ddl-generation" value="create-tables" />
> <property name="eclipselink.ddl-generation.output-mode" value="database" />
> <property name="eclipselink.logging.level" value="FINEST" />
> </properties>
> </persistence-unit>
> </persistence>
>
> Thank you very much.
>
> Regards,
> Jens


--
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
http://www.talend.com

Reply | Threaded
Open this post in threaded view
|

Aw: Re: Updating entities with Aries JPA in Declarative Services

Jens Offenbach
@Christian
That was the missing piece... Thank you very much!

Best regards,
Jens

Gesendet: Dienstag, 18. April 2017 um 10:10 Uhr
Von: "Christian Schneider" <[hidden email]>
An: [hidden email]
Betreff: Re: Updating entities with Aries JPA in Declarative Services
Transaction type JTA means that you need a JTA transaction instead of a
local transaction on the em.
In both cases it is necessary to be in a transaction when you do changes
to the database.

The JPATemplate allows to transparently begin and commit the
transaction. It also demarcates the lifecycle of the em. So outside the
outermost JPATemplate execution the em is closed and your entities are
detached.

Your code without a transaction might work but rather as a side effect
of persistence manager caching. For example hibernate does not directly
write to the database when you change properties.
So simply changing the properties might work outside the transaction.
Still I would not recommend it as you never know when hibernate decides
to write to the db.

Christian

On 18.04.2017 08:59, Jens Offenbach wrote:

> Hallo,
> I have got a question regarding updates of JPA entities using Aries JPA with Declarative Services (DS). Hopefully, someone can help me.
>
> Let's assume we have the Task entity from the example here: https://github.com/apache/aries/blob/trunk/jpa/examples/tasklist-model/src/main/java/org/apache/aries/jpa/example/tasklist/model/Task.java
>
> When e.g. the fields "title" and "description" must be updated, is it mandatory to do it via JpaTemplate within a transaction like this:
>
> jpa.tx(em -> {
> task.setDescription("New description");
> task.setTitle("New title");
> em.flush();
> });
>
> or can safely be done without any transaction like this:
>
> task.setDescription("New description");
> task.setTitle("New title");
>
> I would say, it can be done directly without any transactions on application-level because transaction-type is "JTA" and not "RESOURCE_LOCAL".
>
> persistence.xml:
> <?xml version="1.0" encoding="UTF-8"?>
> <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]
> http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd[http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd]"
> version="2.0">
> <persistence-unit name="treedb" transaction-type="JTA">
>
> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
> <!-- Only used when transaction-type=JTA -->
> <jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/treedb)</jta-data-source>
> <!-- Only used when transaction-type=RESOURCE_LOCAL -->
> <non-jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/treedb)</non-jta-data-source>
>
> <class>...</class>
> <exclude-unlisted-classes>true</exclude-unlisted-classes>
>
> <properties>
> <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.H2Platform" />
>
> <property name="eclipselink.weaving" value = "static"/>
> <property name="eclipselink.weaving.internal" value="true"/>
> <property name="eclipselink.weaving.lazy" value="true" />
> <property name="eclipselink.weaving.changetracking" value="true" />
> <property name="eclipselink.weaving.fetchgroups" value="true" />
> <property name="eclipselink.weaving.eager" value="false" />
>
> <property name="eclipselink.ddl-generation" value="create-tables" />
> <property name="eclipselink.ddl-generation.output-mode" value="database" />
> <property name="eclipselink.logging.level" value="FINEST" />
> </properties>
> </persistence-unit>
> </persistence>
>
> Thank you very much.
>
> Regards,
> Jens


--
Christian Schneider
http://www.liquid-reality.de[http://www.liquid-reality.de]

Open Source Architect
http://www.talend.com[http://www.talend.com]