Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Obtaining EM properties outside a transaction
Obtaining EM properties outside a transaction [message #548184] Wed, 21 July 2010 02:14 Go to next message
Mauro Flores is currently offline Mauro FloresFriend
Messages: 84
Registered: September 2009
Location: Brasil
Member
I need to know, for instance, which kind of database my entity manager is referencing (mysql, oracle..). 

The code below is an aproach: 
Session session = em.getSession();
if (session.getDatasourcePlatform().isOracle()) {
  ....
}


But, I need to put this code inside a method that is not in a transaction context. More precisely, I need to obtain this information inside a EJB method with transaction type supports. 

The code above doesn't work outside a transaction context, because the session is null. Is there a way to obtain this information (or other entity manager property) without a transaction? Can I obtain the session without being inside a transaction context?

Thanks. Mauro.
Re: Obtaining EM properties outside a transaction [message #548728 is a reply to message #548184] Thu, 22 July 2010 18:25 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Same question is posted here:
http://www.eclipse.org/forums/index.php?t=msg&th=172200& amp;start=0&
use getServerSession or getUnitOfWork()

Best Regards,
Chris
Re: Obtaining EM properties outside a transaction [message #548768 is a reply to message #548728] Thu, 22 July 2010 22:36 Go to previous messageGo to next message
Mauro Flores is currently offline Mauro FloresFriend
Messages: 84
Registered: September 2009
Location: Brasil
Member
Chris,

Thanks for the answer. But, I don't think that is the same question. Or I didn't get it.
I don't have a trasaction context.
I put in one EJB method (with support annotations) all alternatives, and none of them worked out.
 @TransactionAttribute(TransactionAttributeType.SUPPORTS)  
  public List<AlunoED> lista(AlunoED alunoED) {
    try {
      Session session = JpaHelper.getEntityManager(em).getSession(); // 1
      System.out.println("session = " + session );
    } catch (Exception e) {
      System.out.println("session - exception " + e.getMessage() );
    }
    try {
      Session serverSession = JpaHelper.getEntityManager(em).getServerSession(); // 2
      System.out.println("serverSession = " + serverSession );
    } catch (Exception e) {
      System.out.println("serverSession - exception " +  e.getMessage() );
    }
    try {
      Session unitOfWork = JpaHelper.getEntityManager(em).getUnitOfWork(); // 3
      System.out.println("unitOfWork  = " + unitOfWork );
    } catch (Exception e) {
      System.out.println("unitOfWork - excepption " + e.getMessage());
    }


Calling this method I have the following display on console:
session - exception null
serverSession = null
unitOfWork - excepption null


If I change the transaction type to Required, then the result change:
session = null
serverSession = ServerSession(
	DatabaseAccessor(connected)
	Oracle10Platform)
unitOfWork  = UnitOfWork(
	DatabaseAccessor(connected)
	Oracle10Platform)
 


But, I don't want to create a transaction context just to obtaing this information in a method that won't update de database.



Re: Obtaining EM properties outside a transaction [message #548967 is a reply to message #548768] Fri, 23 July 2010 14:46 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Hello Mauro,

I have no idea why you would get an exception 2 out of the 3 times you call JpaHelper.getEntityManager(em).getX() without a transaction. I suspect that there is no delegate entitymanager, that the server is returning a proxy and has not deployed or obtained the underlying EclipseLink entityManager yet when you are calling JpaHelper.getEntityManager(em), which would cause the method to return null. But I have no idea why this wouldn't result in a NPE 3/3 times.

Maybe try a simple operation that the proxy would need to use the EclipseLink em for - something like isOpen or contains on the em before trying to get the sessin. Other than that, you will need to consult with the server docs to see how to get the delegate em (if em.getDelegate() is returning null as I suspect).

Best Regards,
Chris
Re: Obtaining EM properties outside a transaction [message #549044 is a reply to message #548967] Fri, 23 July 2010 19:53 Go to previous message
Mauro Flores is currently offline Mauro FloresFriend
Messages: 84
Registered: September 2009
Location: Brasil
Member
Chris

- em.isOpen, em.contains - didn't change anythis.
- getDelegate() - do not return null

  @TransactionAttribute(TransactionAttributeType.SUPPORTS)  
  public List<AlunoED> lista(AlunoED alunoED, PropriedadesLista qp) {
    System.out.println("em is open: " + em.isOpen() );    
    System.out.println("em contains: " + em.contains(alunoED));
    EntityManager emdelegate = (EntityManager) em.getDelegate();
    System.out.println("em delegate: " + emdelegate);
    try {
      Session session = JpaHelper.getEntityManager(emdelegate).getSession(); // 1
      System.out.println("session = " + session );
    } catch (Exception e) {
      System.out.println("session - exception " + e.getMessage() );
    }
    .... 


I'll open a call to Oracle. The application server is weblogic 11g.

Thanks.
Previous Topic:One entity with two sequences
Next Topic:EclipseLink and Hessian - IndirectList problem
Goto Forum:
  


Current Time: Tue Mar 19 06:17:06 GMT 2024

Powered by FUDForum. Page generated in 0.07889 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top