persistence@glassfish.java.net

Re: two connections with a persistence unity

From: Andrei Ilitchev <andrei.ilitchev_at_oracle.com>
Date: Fri, 4 Jul 2008 11:34:23 -0400

In Eclipselink (but not in TopLink Essentials) you can use a single
persistence unit, but use different connections in different entity
managers:
the connection properties passed to createEntityManager method.

Note that it's not the same as using different persistence units.
Say, the persistence connects using "PersistenceUnitUser" and EntityManaget
connects using "EntityManagerUser".
In case of Oracle database each user has a corresponding schema,
in the above scenario the mapped tables all reside in PersistenceUnitUser's
schema
and EntityManagerUser will be able to read / write into
PersistenceUnitUser's schema
(provided EntityManagerUser has the necessary authorizartion in the database
and table qualifier "PersistenceUnitUser" is specified,
so that the generated by Eclipselink sql will look like "select * from
PersistenceUnitUser.EMPLOYEE...").

----- Original Message -----
From: "Tom Ware" <tom.ware_at_oracle.com>
To: <persistence_at_glassfish.dev.java.net>
Sent: Friday, July 04, 2008 11:07 AM
Subject: Re: two connections with a persistence unity


There are definitely some limitations to the workarounds.

You could define a number of persistence units in your persistence.xml that
use
the same classes and metadata.

-Tom

Daniel Benítez wrote:
> Hi Tom,
>
> How I can create multiple versions of my persistence unit? with physical
> files? I can't do that because I must connect with a lot of data base.
>
> Daniel Benítez.
>
>
> --------------------------------------------------
> From: "Tom Ware" <tom.ware_at_oracle.com>
> Sent: Friday, July 04, 2008 3:28 PM
> To: <persistence_at_glassfish.dev.java.net>
> Subject: Re: two connections with a persistence unity
>
>> Hi Daniel,
>>
>> At the moment, TopLink Essentials does not support using connections
>> that are defined differently within the same EntityManagerFactory. Here
>> is the Issue Tracker issue that tracks this feature request:
>>
>> https://glassfish.dev.java.net/issues/show_bug.cgi?id=1575
>>
>> The workaround is to create multiple versions of your persistence unit
>> and use different login information for each.
>>
>> -Tom
>>
>> Daniel Benítez wrote:
>>> Hey everybody, We need help!!!
>>>
>>> For a connection with a Derby data base we are using a glassfish
>>> persistence unit. For that, we have created that persistence unit
>>> through
>>> Netbeans and with:
>>>
>>> protected EntityManager em;
>>> private static EntityManagerFactory emf;
>>>
>>> Properties databaseProperties = new Properties();
>>>
>>> databaseProperties.setProperty(oracle.toplink.essentials.config.TopLinkProperties.JDBC_URL,
>>> jdbcUrl);
>>>
>>> databaseProperties.setProperty(oracle.toplink.essentials.config.TopLinkProperties.JDBC_DRIVER,
>>> "org.apache.derby.jdbc.ClientDriver");
>>>
>>> databaseProperties.setProperty(oracle.toplink.essentials.config.TopLinkProperties.JDBC_USER,
>>> Usuario);
>>>
>>> databaseProperties.setProperty(oracle.toplink.essentials.config.TopLinkProperties.JDBC_PASSWORD,
>>> Contrasenya);
>>>
>>> emf = Persistence.createEntityManagerFactory(Manejador,
>>> databaseProperties);
>>> em = emf.createEntityManager( );
>>>
>>> as a code.
>>>
>>> The problem is that when we want to change the initial data base (using
>>> for that, destroy funtion (
>>>
>>> public void destruir(){
>>> em.clear();
>>> em.close();
>>> em = null;
>>> emf.close();
>>> //emf = null;
>>> }
>>>
>>> ) eliminating the class and establishing a new
>>> connection for this new data base ¡we always obtain a connection with
>>> the
>>> initial data base!!!
>>>
>>> What are we doing wrongly ?
>>>
>>> Is it possible to connect an unique persistence unit to different data
>>> base? How?
>>>
>>> Thanks very much for the help
>>