You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Cole Ferrier <co...@gmail.com> on 2017/04/25 14:53:14 UTC

Different behavior - Tomee Plus 7.0.3 vs Tomee plume 7.0.3 - JTA managed database connections

I have a need to be able to run a custom native sql command against the same
connection that is being used for a JPA query.

i have a JTAManaged datasource declared in my resources.xml, it is
referenced in a persistence.xml for JPA.

@Path("/foo")
@Stateless
public class Foo {

	@PersistenceContext(unitName = "foo")
	private EntityManager entityManager;

	@Resource(name = "foo/myDatabase")
	private javax.sql.DataSource myDB;

	@GET
	public DataObj getAll() {

		DataObj result = null;

		try {

			Connection connection = myDB.getConnection();

//actual alter session statement ommitted
			PreparedStatement ps = connection.prepareStatement("ALTER SESSION
STATEMENT");
			ps.execute();
			ps.close();

			TypedQuery<DataObj> query =
entityManager.createNamedQuery("DataObj.findAll", DataObj.class);
			result = query.getSingleResult();

		} catch (Exception exception) {
			exception.printStackTrace();

		}

		return result;
	}
}

this code works in tomee Plus,  as this is a JTA managed transaction, i get
the same connection for both the direct database connection that is injected
and for the connection that is used by JPA.

If i deploy this to tomee plume (eclipse link).. i believe it is using two
different connections.

when i read:
http://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI#Getting_a_JDBC_Connection_from_an_EntityManager

it makes me think the behavior should be the same with eclipselink.

let me know what further information you would want. and how i can provide.



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Different-behavior-Tomee-Plus-7-0-3-vs-Tomee-plume-7-0-3-JTA-managed-database-connections-tp4681596.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Different behavior - Tomee Plus 7.0.3 vs Tomee plume 7.0.3 - JTA managed database connections

Posted by Cole Ferrier <co...@gmail.com>.
After pounding my head for half a day, trying to build a demo that failed...
i think i figured it out.

when using eclipselink (plume)
if the first thing you do is use the entitymanager and you do a "write"
transaction, then you get the database connection to do something else with
the database, you get that "write" connection.

If you get the database connection first, or you only have ran what it
thinks are read only sql.. then your database connection and your jpa
connection are two different connections.

when using openjpa (tomee plus)
it behaves as i would expect, regardless of what you do or do first, both
the JPA and datasource are the same connection.

It looks like if you are using eclipselink/plume, if you edit yoru
persistence.xml and add:

<property name="eclipselink.jdbc.exclusive-connection.mode" 	value="Always"
/> 
<property name="eclipselink.jdbc.exclusive-connection.is-lazy"
value="false"/>

then you get the openjpa behavior.. and the behavior documented on 
http://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI#Getting_a_JDBC_Connection_from_an_EntityManager

i got the idea for that setting from:

https://wiki.eclipse.org/EclipseLink/Examples/JPA/Auditing
---

based on all of this. i'm not sure if i could call this a Tomee issue... i'm
curious what the jee spec says should happen...






--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Different-behavior-Tomee-Plus-7-0-3-vs-Tomee-plume-7-0-3-JTA-managed-database-connections-tp4681596p4681599.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: Different behavior - Tomee Plus 7.0.3 vs Tomee plume 7.0.3 - JTA managed database connections

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

if the datasource is a JtaManaged=true one and the persistence unit a JTA
one (seems so) then it should reuse the same connection which is stored in
the transaction registry. Do you have a github sample reproducing this
issue (with a failing test or tomee-maven-plugin set up)?


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2017-04-25 16:53 GMT+02:00 Cole Ferrier <co...@gmail.com>:

> I have a need to be able to run a custom native sql command against the
> same
> connection that is being used for a JPA query.
>
> i have a JTAManaged datasource declared in my resources.xml, it is
> referenced in a persistence.xml for JPA.
>
> @Path("/foo")
> @Stateless
> public class Foo {
>
>         @PersistenceContext(unitName = "foo")
>         private EntityManager entityManager;
>
>         @Resource(name = "foo/myDatabase")
>         private javax.sql.DataSource myDB;
>
>         @GET
>         public DataObj getAll() {
>
>                 DataObj result = null;
>
>                 try {
>
>                         Connection connection = myDB.getConnection();
>
> //actual alter session statement ommitted
>                         PreparedStatement ps =
> connection.prepareStatement("ALTER SESSION
> STATEMENT");
>                         ps.execute();
>                         ps.close();
>
>                         TypedQuery<DataObj> query =
> entityManager.createNamedQuery("DataObj.findAll", DataObj.class);
>                         result = query.getSingleResult();
>
>                 } catch (Exception exception) {
>                         exception.printStackTrace();
>
>                 }
>
>                 return result;
>         }
> }
>
> this code works in tomee Plus,  as this is a JTA managed transaction, i get
> the same connection for both the direct database connection that is
> injected
> and for the connection that is used by JPA.
>
> If i deploy this to tomee plume (eclipse link).. i believe it is using two
> different connections.
>
> when i read:
> http://wiki.eclipse.org/EclipseLink/Examples/JPA/EMAPI#Getting_a_JDBC_
> Connection_from_an_EntityManager
>
> it makes me think the behavior should be the same with eclipselink.
>
> let me know what further information you would want. and how i can provide.
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.
> n4.nabble.com/Different-behavior-Tomee-Plus-7-0-3-vs-
> Tomee-plume-7-0-3-JTA-managed-database-connections-tp4681596.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>