You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by tschuler <th...@opentext.com> on 2016/01/11 11:41:33 UTC

Multiple EntityManager using wrong Oracle DataSource

Hi!

We use two Oracle database instances (user1 & user2) running on same
database server available through same service name:

<Resource id="myDatabase1" type="DataSource">
  JdbcDriver oracle.jdbc.OracleDriver
  JdbcUrl jdbc:oracle:thin://localhost:1521/ServiceName
  UserName user1
  Password password1
</Resource>
<Resource id="myDatabase2" type="DataSource">
  JdbcDriver oracle.jdbc.OracleDriver
  JdbcUrl jdbc:oracle:thin:@//localhost:1521/ServiceName
  UserName user2
  Password password2
</Resource>

In persistence.xml we define two persistence units:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
  <persistence-unit name="my-unit1">
    <jta-data-source>myDatabase1</jta-data-source>
  </persistence-unit>
  <persistence-unit name="my-unit2">
    <jta-data-source>myDatabase2</jta-data-source>
  </persistence-unit>
</persistence>

What we observe is that an EntityManager for the second persistence unit
(my-unit2) uses a database connection from the first data source myDatabase1
(instead of myDatabase2 as defined).
All works fine if different jdbc urls are used within tomee.xml (e.g.
different service names or different database server).

Does anyone know of a special setting that enforces usage of correct
datasource for configuration described above?

Best regards,
	Thomas



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Multiple-EntityManager-using-wrong-Oracle-DataSource-tp4677399.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Multiple EntityManager using wrong Oracle DataSource

Posted by Romain Manni-Bucau <rm...@gmail.com>.
if you dont need non-jta-datasource (openjpa.jdbc.SchemaFactory needs it)
then it is not used so any datasource can be set.

the non-jta-datasource is set *by default* by tomee copying the closest
datasource. First time it is the jta datasource since there is no non jta
datasource
but second time (2nd persistence unit) the closest is the previously
created non jta datasource which lead to the behavior you report which is
the
one which is implemented/expected today.

This is also why it is logged: to give the user the information some
resources have been auto adjusted and can need attention.



Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2016-01-11 14:15 GMT+01:00 tschuler <th...@opentext.com>:

> Hi Romain!
>
> Thanks for the hints.
> If both jta- and non-jta-datasources are defined and referenced in
> persistence.xml all works fine.
>
> Maybe you are interested in behaviour for different settings in
> persistence.xml:
> - Only jta-datasources are set and property openjpa.jdbc.SchemaFactory is
> set: problem occurs as described above
> - Only non-jta-datasources are set and property openjpa.jdbc.SchemaFactory
> is set: problem occurs as described above
> - Only jta-datasources are set and no property openjpa.jdbc.SchemaFactory:
> works as expected
> - Only non-jta-datasources are set and no property
> openjpa.jdbc.SchemaFactory: similar (not same) problem occurs as described
> above
>
> Best regards,
> Thomas
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Multiple-EntityManager-using-wrong-Oracle-DataSource-tp4677399p4677404.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.
>

Re: Multiple EntityManager using wrong Oracle DataSource

Posted by tschuler <th...@opentext.com>.
Hi Romain!

Thanks for the hints.
If both jta- and non-jta-datasources are defined and referenced in
persistence.xml all works fine.

Maybe you are interested in behaviour for different settings in
persistence.xml:
- Only jta-datasources are set and property openjpa.jdbc.SchemaFactory is
set: problem occurs as described above
- Only non-jta-datasources are set and property openjpa.jdbc.SchemaFactory
is set: problem occurs as described above
- Only jta-datasources are set and no property openjpa.jdbc.SchemaFactory:
works as expected
- Only non-jta-datasources are set and no property
openjpa.jdbc.SchemaFactory: similar (not same) problem occurs as described
above

Best regards,
Thomas



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Multiple-EntityManager-using-wrong-Oracle-DataSource-tp4677399p4677404.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Multiple EntityManager using wrong Oracle DataSource

Posted by Romain Manni-Bucau <rm...@gmail.com>.
PS: i see a single datasource:

Jan 11, 2016 12:19:25 PM org.apache.openejb.config.AutoConfig
logAutoCreateResource
INFO: Auto-creating a Resource with id 'myDatabaseJta' of type
'DataSource for 'my-unit'.
Jan 11, 2016 12:19:25 PM org.apache.openejb.config.AutoConfig deploy
INFO: Configuring Service(id=myDatabaseJta, type=Resource,
provider-id=myDatabase)
Jan 11, 2016 12:19:25 PM
org.apache.openejb.assembler.classic.Assembler createRecipe
INFO: Creating Resource(id=myDatabaseJta)
Jan 11, 2016 12:19:25 PM
org.apache.tomee.jdbc.TomEEDataSourceCreator$TomEEDataSource readOnly
INFO: Disabling testOnBorrow since no validation query is provided
Jan 11, 2016 12:19:25 PM org.apache.openejb.config.AutoConfig setJtaDataSource
INFO: Adjusting PersistenceUnit my-unit <jta-data-source> to Resource
ID 'myDatabaseJta' from 'null'
Jan 11, 2016 12:19:25 PM org.apache.openejb.config.AutoConfig deploy
INFO: Configuring PersistenceUnit(name=my-unit2)
Jan 11, 2016 12:19:25 PM org.apache.openejb.config.AutoConfig setJtaDataSource
INFO: Adjusting PersistenceUnit my-unit2 <jta-data-source> to Resource
ID 'myDatabaseJta' from 'null'



Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2016-01-11 13:05 GMT+01:00 Romain Manni-Bucau <rm...@gmail.com>:

> I guess it is the non jta database auto setting, can you duplicate your
> datasource set JtaManaged = false in the copies and set them on
> non-jta-datasource in both persistence units?
>
>
> Romain Manni-Bucau
> @rmannibucau <https://twitter.com/rmannibucau> |  Blog
> <http://rmannibucau.wordpress.com> | Github
> <https://github.com/rmannibucau> | LinkedIn
> <https://www.linkedin.com/in/rmannibucau> | Tomitriber
> <http://www.tomitribe.com>
>
> 2016-01-11 12:42 GMT+01:00 tschuler <th...@opentext.com>:
>
>> Hi Romain!
>>
>> The wrong database connection usage only happens if both persistence units
>> are injected into a session bean.
>> It does not matter if jta or non-jta data sources are used.
>> Attached catalina log  catalina.log
>> <http://tomee-openejb.979440.n4.nabble.com/file/n4677401/catalina.log>
>> including stack trace.
>>
>> Digging deeper into the problem I can see that setting the persistence
>> unit
>> property openjpa.jdbc.SchemaFactory is needed for provocating the problem:
>>
>>   <persistence-unit name="my-unit1">
>>     <jta-data-source>myDatabase1</jta-data-source>
>>     <properties>
>>       *<property name="openjpa.jdbc.SchemaFactory"
>> value="native(ForeignKeys=true)" />*
>>     </properties>
>>   </persistence-unit>
>>   <persistence-unit name="my-unit2">
>>     <jta-data-source>myDatabase2</jta-data-source>
>>     <properties>
>>       *<property name="openjpa.jdbc.SchemaFactory"
>> value="native(ForeignKeys=true)" />*
>>     </properties>
>>   </persistence-unit>
>>
>> Sorry for providing incomplete information at first.
>>
>> Can anyone explain why setting the property causes usage of wrong database
>> connection for the second entity manager?
>>
>> Best regards,
>>         Thomas
>>
>>
>>
>> --
>> View this message in context:
>> http://tomee-openejb.979440.n4.nabble.com/Multiple-EntityManager-using-wrong-Oracle-DataSource-tp4677399p4677401.html
>> Sent from the TomEE Dev mailing list archive at Nabble.com.
>>
>
>

Re: Multiple EntityManager using wrong Oracle DataSource

Posted by Romain Manni-Bucau <rm...@gmail.com>.
I guess it is the non jta database auto setting, can you duplicate your
datasource set JtaManaged = false in the copies and set them on
non-jta-datasource in both persistence units?


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2016-01-11 12:42 GMT+01:00 tschuler <th...@opentext.com>:

> Hi Romain!
>
> The wrong database connection usage only happens if both persistence units
> are injected into a session bean.
> It does not matter if jta or non-jta data sources are used.
> Attached catalina log  catalina.log
> <http://tomee-openejb.979440.n4.nabble.com/file/n4677401/catalina.log>
> including stack trace.
>
> Digging deeper into the problem I can see that setting the persistence unit
> property openjpa.jdbc.SchemaFactory is needed for provocating the problem:
>
>   <persistence-unit name="my-unit1">
>     <jta-data-source>myDatabase1</jta-data-source>
>     <properties>
>       *<property name="openjpa.jdbc.SchemaFactory"
> value="native(ForeignKeys=true)" />*
>     </properties>
>   </persistence-unit>
>   <persistence-unit name="my-unit2">
>     <jta-data-source>myDatabase2</jta-data-source>
>     <properties>
>       *<property name="openjpa.jdbc.SchemaFactory"
> value="native(ForeignKeys=true)" />*
>     </properties>
>   </persistence-unit>
>
> Sorry for providing incomplete information at first.
>
> Can anyone explain why setting the property causes usage of wrong database
> connection for the second entity manager?
>
> Best regards,
>         Thomas
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Multiple-EntityManager-using-wrong-Oracle-DataSource-tp4677399p4677401.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.
>

Re: Multiple EntityManager using wrong Oracle DataSource

Posted by tschuler <th...@opentext.com>.
Hi Romain!

The wrong database connection usage only happens if both persistence units
are injected into a session bean.
It does not matter if jta or non-jta data sources are used.
Attached catalina log  catalina.log
<http://tomee-openejb.979440.n4.nabble.com/file/n4677401/catalina.log>  
including stack trace.

Digging deeper into the problem I can see that setting the persistence unit
property openjpa.jdbc.SchemaFactory is needed for provocating the problem:

  <persistence-unit name="my-unit1">
    <jta-data-source>myDatabase1</jta-data-source>
    <properties>
      *<property name="openjpa.jdbc.SchemaFactory"
value="native(ForeignKeys=true)" />*
    </properties>        
  </persistence-unit>
  <persistence-unit name="my-unit2">
    <jta-data-source>myDatabase2</jta-data-source>
    <properties>
      *<property name="openjpa.jdbc.SchemaFactory"
value="native(ForeignKeys=true)" />*
    </properties>        
  </persistence-unit>

Sorry for providing incomplete information at first.

Can anyone explain why setting the property causes usage of wrong database
connection for the second entity manager?

Best regards,
	Thomas



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/Multiple-EntityManager-using-wrong-Oracle-DataSource-tp4677399p4677401.html
Sent from the TomEE Dev mailing list archive at Nabble.com.

Re: Multiple EntityManager using wrong Oracle DataSource

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

which usage is corrupted: the jta one or the non jta one? if last one maybe
just configure non jta datasource if not can you share your logs? something
should be written there.


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2016-01-11 11:41 GMT+01:00 tschuler <th...@opentext.com>:

> Hi!
>
> We use two Oracle database instances (user1 & user2) running on same
> database server available through same service name:
>
> <Resource id="myDatabase1" type="DataSource">
>   JdbcDriver oracle.jdbc.OracleDriver
>   JdbcUrl jdbc:oracle:thin://localhost:1521/ServiceName
>   UserName user1
>   Password password1
> </Resource>
> <Resource id="myDatabase2" type="DataSource">
>   JdbcDriver oracle.jdbc.OracleDriver
>   JdbcUrl jdbc:oracle:thin:@//localhost:1521/ServiceName
>   UserName user2
>   Password password2
> </Resource>
>
> In persistence.xml we define two persistence units:
>
> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
>   <persistence-unit name="my-unit1">
>     <jta-data-source>myDatabase1</jta-data-source>
>   </persistence-unit>
>   <persistence-unit name="my-unit2">
>     <jta-data-source>myDatabase2</jta-data-source>
>   </persistence-unit>
> </persistence>
>
> What we observe is that an EntityManager for the second persistence unit
> (my-unit2) uses a database connection from the first data source
> myDatabase1
> (instead of myDatabase2 as defined).
> All works fine if different jdbc urls are used within tomee.xml (e.g.
> different service names or different database server).
>
> Does anyone know of a special setting that enforces usage of correct
> datasource for configuration described above?
>
> Best regards,
>         Thomas
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/Multiple-EntityManager-using-wrong-Oracle-DataSource-tp4677399.html
> Sent from the TomEE Dev mailing list archive at Nabble.com.
>