Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] connecting with different usernames inthesame process

((AbstractSession)entityManagerImpl.getActiveSession()).getParent().getAccessor();

Note that the accessor will be there only after it's has been used (query executed or transaction started).

If you need to access connection before it has been used, handle postAcquireConnection or postAcquireExclusiveConnection event.

----- Original Message -----
Sent: Monday, April 20, 2009 4:42 PM
Subject: Re: [eclipselink-users] connecting with different usernames inthesame process

Thanks. In that first case, how do I get my hands on the underlying Connection? I don't see on the ServerSession interface a way to get to it other than via pool?

On Apr 20, 2009, at 4:28 PM, Andrei Ilitchev wrote:

You can create a new EntityManager with individual DataSource or user/password/connection String by passing properties to createEM method:
  HashMap props = new HashMap();
  props.put("javax.persistence.jdbc.user", "MyUser");
  props.put("javax.persistence.jdbc.password", "MyPassword");
  props.put("javax.persistence.jdbc.url", "MyConnectionString");
  props.put("eclipselink.jdbc.exclusive-connection.mode", "Always");
  emf.createEntityManager("MyPU", props);
 
Note that in the above case no connection pooling used - just a single connection is created for the entity manager (which is closed when em is closed).
 
Alternatively you can create one or more Eclipselink's ConnectionPools and add them to ServerSession in SessionCustomizer - all of them will connect when the first entity manager is created.
You'll need then to create ConnectionPolicy for the pool you want to use:
  myConnectionPolicy = new ConnectionPolicy("MyPool");
then pass it to createEM method as a property (instead of user/password/url properties):
  props.put("eclipselink.jdbc.connection-policy", myConnectionPolicy);
 
 
----- Original Message -----
From: "David Parker" <dap@xxxxxxxxxxxxxxxxxxxx>
To: "EclipseLink User Discussions" <eclipselink-users@xxxxxxxxxxx>
Sent: Monday, April 20, 2009 3:48 PM
Subject: Re: [eclipselink-users] connecting with different usernames in thesame process

>I guess I actually DO have an issue: there is only one connection  
> pool, though I have created two different entitymanagers with  
> different connection properties. The connection pool is associated  
> with the first set of connection properties.
> 
> The functionality I'm trying to implement is creating a new database  
> user/schema while connected to an existing db/schema/persistenceUnit.
> 
> The EntityManagerFactory I'm using is a static. Does the a different  
> database user imply a new EntityManagerFactory entirely, or is there a  
> way to specify a named pool when creating a new EntityManager?
> 
> thx
> 
> On Apr 20, 2009, at 3:02 PM, David Parker wrote:
> 
>> Never mind. I had an EntityManagerFactory being created earlier in  
>> the process, and wasn't passing the new properties to get  
>> entitymanager2. Not so subtle!
>>
>> /thx
>>
>> On Apr 20, 2009, at 1:10 PM, David Parker wrote:
>>
>>> In my application I need to make connections to 2 separate database  
>>> usernames. I get the main entitymanager1,then create a second  
>>> entitymanager1 with a different username. I need to get a  
>>> Connection from the second entitymanager. I am using code like this:
>>>
>>>  private Accessor getAccessor() {
>>>       EntityManagerImpl em = (EntityManagerImpl) entityManager2;
>>>       session = em.getServerSession();
>>>       Accessor accessor =  
>>> session.getDefaultConnectionPool().acquireConnection();
>>>       accessor.incrementCallCount(session);
>>>       return accessor;
>>>   }
>>>
>>> The Connection I'm ending up with is for the username from  
>>> entitymanager1, where I need a Connection from entitymanager2.
>>>
>>> Is this because I am using getDefaultConnectionPool? How should I  
>>> handle different database connections in this case?
>>>
>>> Thanks.
>>> =============================================
>>> David Parker
>>> 
dap@xxxxxxxxxxxxxxxxxxxx
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> eclipselink-users mailing list
>>> 
eclipselink-users@xxxxxxxxxxx
>>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>>>
>>
>> =============================================
>> David Parker
>> 
dap@xxxxxxxxxxxxxxxxxxxx
>>
>>
>>
>>
>> _______________________________________________
>> eclipselink-users mailing list
>> 
eclipselink-users@xxxxxxxxxxx
>> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>>
> 
> =============================================
> David Parker
> 
dap@xxxxxxxxxxxxxxxxxxxx
> 
> 
> 
> 
> _______________________________________________
> eclipselink-users mailing list
> 
eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> _______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

=============================================
David Parker





_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Back to the top