Topic
  • 11 replies
  • Latest Post - ‏2011-07-05T14:34:00Z by leealber
infyRavi
infyRavi
14 Posts

Pinned topic javax.persistence.TransactionRequiredException:no transaction is in progres

‏2011-06-23T14:21:35Z |
Hi,
We are using the following technology stack for JPA 1.0 implementation in our application.

WAS : 7.0.0.11
Spring : 3.0.5
Hibernate : 3.3.2

Whenever we are trying to persist the data we are getting the following error.
"javax.persistence.TransactionRequiredException:no transaction is in progress".

We are using container -managed transaction management.
we are injecting entityManager using @persistenceContext.

Few observations

Within the method where we are perfoming em.persist we are also calling em.find and we are getting the
data.After em.persist we are calling em.flush when we get the above error.
if we remove flush() then no error but data is not committed.

I have attached the doc which talks about our datasource configuration,changing JPA default implementor in WAS admin console, our persistence.xml file, spring configuration file and also the code where it is failing.

I am not able to find where actually we are going wrong and why we are getting this error.

Thanks
Ravi
Updated on 2011-07-05T14:34:00Z at 2011-07-05T14:34:00Z by leealber
  • infyRavi
    infyRavi
    14 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-06-23T14:24:10Z  
    Attachment
  • leealber
    leealber
    19 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-06-23T15:00:39Z  
    Ravi,

    The attachment did not seem to exist in this posting thread. Can you try again.

    To be clear, you are using WAS 7 (NOT JPA FeP) and define a JPA 1.0 (Hibernate) third party persistence provider.
    Using injection to get the EntityManager and persist/flush/find using this em. What component type is the injection is declared? (i.e. EJB, servlet)

    When and where the TransactionRequiredException takes place? Who is throwing the exception. A exception stack may provide some insight to the problem.

    If using EJB, take a trace with EJBContainer=all:JPA=all:Injection=all would give you some idea on the CMT boundary, injection and container managed persistence processings.

    Albert Lee.
  • sutter
    sutter
    94 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-06-23T15:14:47Z  
    Hi Ravi,
    When you get the javax.persistence.TransactionRequiredException, have you looked at the call stack to ensure that you are using Hibernate (vs the built-in OpenJPA provider)? If you have, then at least you have configured your Hibernate usage correctly.

    From a generic JPA perspective, the TransactionRequiredException means just that. You need a Transaction in order to complete the operation. This is not a JPA provider "error". Since you are using Spring in your WebSphere environment, the lack of a proper Transaction context may be with your Spring configuration and usage (rather than WebSphere).

    I think you need to analyze how you expect your transactions to be managed. You mention container-managed transactions, but are you expecting them to be Spring managed, or WebSphere managed? Here is one reference that provides that some general guidance on using Spring with WebSphere [1], but depending on what you find out, you may have to consult with the Spring forums as well. Good luck.

    [1] http://www.ibm.com/developerworks/websphere/techjournal/0609_alcott/0609_alcott.html

    P.S. Your attachment didn't work for this message, nor the follow-on message...

    Kevin Sutter, JPA Architect, IBM WebSphere
  • infyRavi
    infyRavi
    14 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-06-23T17:42:26Z  
    • sutter
    • ‏2011-06-23T15:14:47Z
    Hi Ravi,
    When you get the javax.persistence.TransactionRequiredException, have you looked at the call stack to ensure that you are using Hibernate (vs the built-in OpenJPA provider)? If you have, then at least you have configured your Hibernate usage correctly.

    From a generic JPA perspective, the TransactionRequiredException means just that. You need a Transaction in order to complete the operation. This is not a JPA provider "error". Since you are using Spring in your WebSphere environment, the lack of a proper Transaction context may be with your Spring configuration and usage (rather than WebSphere).

    I think you need to analyze how you expect your transactions to be managed. You mention container-managed transactions, but are you expecting them to be Spring managed, or WebSphere managed? Here is one reference that provides that some general guidance on using Spring with WebSphere [1], but depending on what you find out, you may have to consult with the Spring forums as well. Good luck.

    [1] http://www.ibm.com/developerworks/websphere/techjournal/0609_alcott/0609_alcott.html

    P.S. Your attachment didn't work for this message, nor the follow-on message...

    Kevin Sutter, JPA Architect, IBM WebSphere
    Here goes the attachment
  • sutter
    sutter
    94 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-06-23T20:45:09Z  
    • infyRavi
    • ‏2011-06-23T17:42:26Z
    Here goes the attachment
    Hi Ravi,
    Thanks for the attachment. I'm not an expert with Spring and Hibernate, but nothing is jumping out at me that is wrong with your configuration.

    But, it is very clear from your callstack that you are using Spring and Hibernate to control and interact with your transactions. So, that's good -- no interference by WebSphere or OpenJPA.

    Either the @transactional is not properly demarcating a WebSphere transaction, or the Hibernate EntityManager is not properly detecting this transaction and associating the EM with the transaction. In either case, I think you should run this problem by a Spring or Hibernate forum to find out why the transaction is not being properly detected.

    Or, as Albert pointed out in his reply, you could turn on more WebSphere tracing and debug further from that viewpoint. At least this would show whether a transaction is ever started or not. But, you may also need more tracing from Spring and/or Hibernate to really complete that picture as well.

    Good luck,
    Kevin Sutter, JPA Architect, IBM WebSphere
  • infyRavi
    infyRavi
    14 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-06-25T12:05:47Z  
    • sutter
    • ‏2011-06-23T20:45:09Z
    Hi Ravi,
    Thanks for the attachment. I'm not an expert with Spring and Hibernate, but nothing is jumping out at me that is wrong with your configuration.

    But, it is very clear from your callstack that you are using Spring and Hibernate to control and interact with your transactions. So, that's good -- no interference by WebSphere or OpenJPA.

    Either the @transactional is not properly demarcating a WebSphere transaction, or the Hibernate EntityManager is not properly detecting this transaction and associating the EM with the transaction. In either case, I think you should run this problem by a Spring or Hibernate forum to find out why the transaction is not being properly detected.

    Or, as Albert pointed out in his reply, you could turn on more WebSphere tracing and debug further from that viewpoint. At least this would show whether a transaction is ever started or not. But, you may also need more tracing from Spring and/or Hibernate to really complete that picture as well.

    Good luck,
    Kevin Sutter, JPA Architect, IBM WebSphere
    Hi Sutter,
    Do we need to configure XADatasource for using transaction type ="JTA" in JPA.
    We are using normal JDBC drivers.
    Apart from single database we are not having any other resources.
    so please suggest.

    Thanks
    Ravi
  • SystemAdmin
    SystemAdmin
    45 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-06-27T14:06:37Z  
    • infyRavi
    • ‏2011-06-25T12:05:47Z
    Hi Sutter,
    Do we need to configure XADatasource for using transaction type ="JTA" in JPA.
    We are using normal JDBC drivers.
    Apart from single database we are not having any other resources.
    so please suggest.

    Thanks
    Ravi
    Hi Ravi,

    You may use an XA DataSource or a non-XA DataSource when you use transaction type = "JTA". Both types of DataSource can participate in a JTA transaction.

    Hope this helps,
    -mike
  • SystemAdmin
    SystemAdmin
    45 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-06-27T14:06:37Z  
    • infyRavi
    • ‏2011-06-25T12:05:47Z
    Hi Sutter,
    Do we need to configure XADatasource for using transaction type ="JTA" in JPA.
    We are using normal JDBC drivers.
    Apart from single database we are not having any other resources.
    so please suggest.

    Thanks
    Ravi
    Hi Ravi,

    You may use an XA DataSource or a non-XA DataSource when you use transaction type = "JTA". Both types of DataSource can participate in a JTA transaction.

    Hope this helps,
    -mike
  • infyRavi
    infyRavi
    14 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-07-01T09:58:40Z  
    Hi Ravi,

    You may use an XA DataSource or a non-XA DataSource when you use transaction type = "JTA". Both types of DataSource can participate in a JTA transaction.

    Hope this helps,
    -mike
    Hi Mike,
    In WAS admin console I was searching to find the JNDI mapping of JTA transaction and also the entityManagerFactory, but didn't succeed, can you please let me know where can I find it.
    I am using WAS 7.x

    Thanks
    Ravi
  • infyRavi
    infyRavi
    14 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-07-02T18:20:46Z  
    • infyRavi
    • ‏2011-07-01T09:58:40Z
    Hi Mike,
    In WAS admin console I was searching to find the JNDI mapping of JTA transaction and also the entityManagerFactory, but didn't succeed, can you please let me know where can I find it.
    I am using WAS 7.x

    Thanks
    Ravi
    Hi,
    In WAS console I am able to see the following entries

    7/1/11 20:16:25:646 IST 00000000 JPAComponentI I CWWJP0026I: The Java Persistence API (JPA) component is initializing.
    7/1/11 20:16:25:646 IST 00000000 JPAComponentI I CWWJP0006I: The org.hibernate.ejb.HibernatePersistence class is loaded as the default Java Persistence API (JPA) provider.
    7/1/11 20:16:25:662 IST 00000000 JPAComponentI I CWWJP0027I: The Java Persistence API (JPA) component has initialized.

    7/1/11 20:16:28:083 IST 00000000 JPAComponentI I CWWJP0028I: The Java Persistence API (JPA) component is starting.
    7/1/11 20:16:28:099 IST 00000000 JPAComponentI I CWWJP0017I: The Java Persistence API (JPA) component has started.

    Does it mean Hibernate is picked up as default JPA provider or not.
    Because the last 2 entries are confusing me.
    Please clear my doubt.

    Thanks
    Ravi
  • leealber
    leealber
    19 Posts

    Re: javax.persistence.TransactionRequiredException:no transaction is in progres

    ‏2011-07-05T14:34:00Z  
    • infyRavi
    • ‏2011-07-02T18:20:46Z
    Hi,
    In WAS console I am able to see the following entries

    7/1/11 20:16:25:646 IST 00000000 JPAComponentI I CWWJP0026I: The Java Persistence API (JPA) component is initializing.
    7/1/11 20:16:25:646 IST 00000000 JPAComponentI I CWWJP0006I: The org.hibernate.ejb.HibernatePersistence class is loaded as the default Java Persistence API (JPA) provider.
    7/1/11 20:16:25:662 IST 00000000 JPAComponentI I CWWJP0027I: The Java Persistence API (JPA) component has initialized.

    7/1/11 20:16:28:083 IST 00000000 JPAComponentI I CWWJP0028I: The Java Persistence API (JPA) component is starting.
    7/1/11 20:16:28:099 IST 00000000 JPAComponentI I CWWJP0017I: The Java Persistence API (JPA) component has started.

    Does it mean Hibernate is picked up as default JPA provider or not.
    Because the last 2 entries are confusing me.
    Please clear my doubt.

    Thanks
    Ravi
    Ravi,

    >> Does it mean Hibernate is picked up as default JPA provider or not.

    Yes, Hibernate is set as the default provider now.

    >> Because the last 2 entries are confusing me.

    There are 2 phases in bringing up a service component in WAS, initialization and start; hence the 2 different messages. The same pattern occurs for the default OpenJPA provider. You can enable trace spec JPA=all to see more details on the start up process.

    Albert Lee.