Wednesday, January 17, 2007

Accessing the Session in TopLink Essentials

Although TopLink Essentials is the reference implementation for JPA, it was derived from Oracle TopLink and therefore shares the same core concepts and classes. Recently I was writing some (admittedly bizarre) JPA code and wanted to verify what was in the shared cache. Of course there's no way to look in the TopLink shared cache using JPA so I needed to use the TopLink API. Fortuately, it's still there if you need it.

To obtain a reference to a session in TopLink Essentials(as in Oracle TopLink) you use the SessionManager. Once you have the session you need, you can use it as you would in Oracle TopLink:

Session session = SessionManager.getManager().getSession("library");
List books = session.readAllObjects(BookImpl.class);
assertEquals("num books", 1, books.size());

The "library" session is the TopLink object underlying the JPA "library" persistence unit as defined in my persistence.xml. But, to make the library session available through the SessionManager you have to add a property to your persistence unit:

<?xml version="1.0" encoding="UTF-8"?>
  <persistence-unit name="library">
      <property name="toplink.session-name" value="library">

By adding this declaration you're effectively publishing your "library" persistence unit under a session name of "library". You could publish your persistence unit under a different session name but why confuse matters.


ArKZoYD said...

I've looked at you first post with hope ! The second is... what I was expecting ! Please continue blogging.

If TopLink is underlying Toplink Essentials, do you think possible to write java callback in order to propagate information such as identity, module, application to the database ? à la

Bert Regards,


Shaun Smith said...

TopLink Essentials is derived from Oracle TopLink and, at a minimum includes the functionality necessary to implement the JPA. Of course, as I point out, the core TopLink classes are included (albeit with a package rename and updates to accomodate JPA requirements) so functionality that is not required for JPA is available just because it was already there in Oracle TopLink.

Unfortunately, the functionality you're looking for isn't in Essentials.


ezgoer said...

Thanks for the info on accessing the Session. A newbie to Toplink Essentials. Is there any way to use batch writing to the database? I have seen examples using ToplinkUtils to access the session and then call the method useBatchWriting(), but access does not seem to be available in essentials. I need to persist and POJO that is about 1M and it is taking about 30 seconds, which seems way too long. Any help would be greatly appreciated.

Shaun Smith said...

Batch writing isn't available in Essentials. But if you're only persisting one very large POJO it wouldn't help. I'm guessing your class has one or more LOBs to be that big?


Bruno said...


I'm trying to do the reverse of what you explained here. I want to be able to obtain an EntityManager from an existing ServerSession. My application configures the ServerSession directly, and all the descriptors are configured in code. I now want to be able to use the JPA interfaces for data access, but defer migrating all the mappings until some time later. Can this be done with TopLink It looks like we need to add the toplink-essentials.jar just to get the actual JPA code, but this all depends on the package oracle.toplink.essentials, while we still have many dependencies on the original package path.

Thanks in advance,

Shaun Smith said...

Hi Bruno,

I posted some comments in a top level blog. The question was just too big to answer in a reply :-).


annerose said...

These comments have been invaluable to me as is this whole site. I thank you for your comment.

Jose Luis said...

Hi, I need know, When Can I use TopLink esentials and When Can I use TopLink JPA?, with both I can query to data base

Shaun Smith said...

TopLink Essentials was the JPA implementation shipped as part of Oracle TopLink 10.1.3.x. EclipseLink is the JPA implmentation shipped in Oracle TopLink 11g. The JPA implementation you are using depends on which version of Oracle TopLink you've got.

Jose Luis said...

Thanks Smith, I'm working with Oracle Databse 10g and Oracle WebLogic Server 10, I've got to make a Architecture of a Enterprise Application. I'm going to use Oracle TopLink (session.xml) for Querys to Local Database, and for EJB's and WebServices to use JPA.
What do you think of this?... thanks

Shaun Smith said...

If you're starting a new application I would use JPA and not TopLink's native API (which has the sessions.xml config file). TopLink 11g (based on EclipseLink--the successor to TopLink Essentials) is certified on WebLogic 10. JPA is part of the EJB3 specification so the integration is very good. I would recommend this approach.

Jose Luis said...

Thanks Smith for your comment