Conundrum Solved: Spring, JPA/Hibernate and WebSphere 7

May 12th, 2010 Via Tsuji, Consultant  (email the author)

We have all been in projects where we use a combination of open source libraries, for different components of our architecture. We know theoretically that it should work, but alas, something blows up at runtime. If we’re lucky, we experience this on a weekly basis, but we don’t get lucky that often. The “Conundrum Solved” series will tackle some of these hair-pulling, sleep-depriving, grouch-inducing problems some of us at Summa have encountered in our projects. We hope that by sharing them with you, you’ll keep your hair, get enough sleep, and maintain a smile on your face!

Problem: Invalidly getting an InvalidStateObjectStateException thrown upon calling  merge(), even if there were no updates to the entity.

Background: I am not alien to the Spring, JPA/Hibernate, and WebSphere combination. But on a recent project, using WebSphere 7 threw us for a loop with a persistent InvalidStateObjectStateException being thrown. Everything was running fine, until I remembered that I forgot to add a version number for optimistic locking.

I tried a version number of type Integer and even timestamps and I constantly got an InvalidStateObjectStateException in the before_completion synchronization. At the same time, I received a java.lang.UnsupportedOperationException at org.hibernate.transaction.WebSphereExtendedJTATransactionLookup$TransactionManagerAdapter.setRollbackOnly.

We were using WebSphere Application Server v7.0.0.7, Spring 2.5.6 and Hibernate 3.3.2.GA.

Solution: With local unit tests running green and my code exclusively updating the database,  I was 100% sure that the error being spewed on the server was invalid. After several tries of trace logging and configuration tweaking, I found the following solution combo:

  • Don’t forget to configure the classloader for the EJB jar as parent-last! Though this wasn’t the cause of the error, you simply can’t forget it.
  • Make sure you specify Hibernate as the JPA provider in the persistence.xml. WebSphere 7 uses OpenJPA, by default. Technically, this didn’t relate to the problem, since I already had this set up, but you would run into an error if you didn’t do so.
    <persistence-unit name="summa-PU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>jdbc/summaDS</jta-data-source>
    </persistence-unit>
  • Also, make sure that you configure org.springframework.transaction.jta.WebSphereUowTransactionmanager as your transaction manager in Spring. Again, unrelated to the problem, but critical to the solution.
  • The UnsupportedOperationException was resolved by changing my hibernate.transaction.manager_lookup_class toorg.hibernate.transaction.WebSphereTransactionManagerLookup, instead of its extended cousin. This seems to work better with WebSphere 7.
  • Turning up the logging levels showed me that the before_completion cache synchronization that Hibernate was doing was being called twice. To resolve, I had to set the Hibernate property, hibernate.transaction.flush_before_completion, to false. It seems that it was true, by default and caused the double flushes.

After flipping different switches and turning various knobs, it’s hard to determine which one tweak resolved the primary error I encountered. But I never saw it again after doing all of the above.

References: Here are some of the articles / posts I used while researching this puzzler.

  1. WebSphere Application Server 7.0 InfoCenter: Using third-party persistence providers.
  2. developerWorks: Using Spring and Hibernate with WebSphere Application Server.
Share and Enjoy:
  • Digg
  • Reddit
  • del.icio.us
  • Google
  • description
  • LinkedIn

Entry Filed under: Agile and Development, Uncategorized

9 Comments Add your own

  • 1. mihaela  |  June 10th, 2010 at 2:52 pm

    Hello,

    Do you know if Hibernate 3.5.0-Final may be used as JPA provider on WebSphere 7.0.0.7? I am working on a project with seam 2.2.0, hibernate 3.5.0-Final, DB2 as database server and Websphere 7.0.0.7 and I cannot obtain an EntityManager injected with @PersistenceContext. Until now we have worked with the Seam EntityManager but we are facing strange behaviour when inserting data with ManyToMany relations and OneToMany with join tables. It doesn’t insert the values in the join table.

    I don’t know where is the problem. Any hints will be helpful.
    Thanks in advance.

  • 2. Via Bulatao  |  June 10th, 2010 at 4:47 pm

    Hi mihaela,

    WebSphere 7.0.0.7 supports Java EE 5, which includes JPA 1.0. Hibernate 3.5.0-Final uses JPA 2.0. Therefore, with just the Hibernate JARs in your application, I don’t think it will work with WebSphere 7.x. I checked the Hibernate Entity Manager documentation for 3.5 (http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html_single/#configuration) and they have provided a Hibernate JPA JAR that you would need to add to your classpath, if you’re not running on a JEE6 server. Just to mention, it would be difficult to remove the JPA JAR from WebSphere, since there is none. The persistence API is bundled in with j2ee.jar.

    I did take the liberty to plug in Hibernate 3.5.0-Final into our existing configuration (without the JPA JAR) and the application did fail to run. Unfortunately, I did not have enough time today to try and muck with the classpaths. I’ll let you know when I get the chance.

    Good luck! Let me know how it goes.

    Via

  • 3. mihaela  |  June 11th, 2010 at 2:58 am

    Thank you for the explanation. I’ll let you know if we can make them work together.

    Mihaela

  • 4. Mike  |  June 13th, 2010 at 6:25 am

    Hello
    I am working on using the same but receive an error,on creating an entitymanagerfactory I am getting an error: methods with same signature createEntityManager() but incompatible return types: [interface com.ibm.websphere.persistence.WsJpaEntityManager, interface org.apache.openjpa.persistence.OpenJPAEntityManagerSPI]
    thanks

  • 5. Via Bulatao  |  June 14th, 2010 at 1:39 pm

    Hi Mike,

    If you’re using Hibernate and getting that error message, make sure you’re doing the 2nd bullet point in the solution above. WebSphere uses OpenJPA, by default.

    Good luck!

    Via

  • 6. Rodrigo  |  October 26th, 2010 at 12:04 pm

    I have the same problem (Hibernate 3.5, Spring 3..0.2) and i need to use Websphere 7.
    I need this version of hibernate. Do you have any idea?

  • 7. Via Bulatao  |  October 27th, 2010 at 8:19 am

    Hey Rodrigo! All the steps I took to resolve our issue is written here. Have you tried the steps outlined above? What errors did you find?

  • 8. Jignesh  |  March 30th, 2011 at 4:00 pm

    If any body uses hibernate 3.5 and WAS then they should use the version 7.0.0.15 which can support JPA2.0 as hibernate 3.5 uses JPA2.0.

    -Jignesh

  • 9. Alejandro  |  July 17th, 2011 at 10:56 pm

    Hello, sorry for my English, I am currently trying to work with websphere and hibernate and unity persistence, I have everything configured properly and is the EntityManagerFactory is created, but what I do any changes to persistence.xml is damaged and says he can not create the EntityManagerFactory.

    any idea.

    Ty.

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Pages

Categories

Most Recent Posts

Feeds

  Subscribe in a reader

Calendar

May 2010
M T W T F S S
« Apr   Jun »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Tags