Search code examples
javahibernatejpaapache-camelopenjpa

org.apache.openjpa.persistence.InvalidStateException: This broker is not configured to use managed transactions


I am trying to configure camel jpa component and when I run the test case, I am getting this error

"org.apache.openjpa.persistence.InvalidStateException: This broker is not configured to use managed transactions."

But If I am not using any transaction manager , it takes the default one and everything goes good

My Configuration

`@JndiBind("jpa")
@Provides*/
@Singleton
public JpaComponent getJpa(JpaVendorAdapter vendorAdapter){
    JpaComponent jpa = new JpaComponent();
    LocalEntityManagerFactoryBean fBean = new LocalEntityManagerFactoryBean();
    fBean.setJpaVendorAdapter(vendorAdapter);
    fBean.setPersistenceUnitName("camel");
    fBean.afterPropertiesSet();
    EntityManagerFactory entityManagerFactory = fBean.getNativeEntityManagerFactory();
    JpaTransactionManager txMgr = new JpaTransactionManager();
    txMgr.setEntityManagerFactory(entityManagerFactory);

    jpa.setEntityManagerFactory(entityManagerFactory);

    jpa.setTransactionManager(txMgr);

    return jpa;
}`

Solution

  • The problem is with getting the object of EntityManager in your code.

        EntityManagerFactory entityManagerFactory = fBean.getObject();
    
        JpaTransactionManager txMgr = new JpaTransactionManager();
        txMgr.setEntityManagerFactory(entityManagerFactory);
        txMgr.afterPropertiesSet();
    
        jpa.setEntityManagerFactory(entityManagerFactory);
        jpa.setTransactionManager(txMgr);