Hello All,
We have a Web Application wherein we have used EclipseLink + JPA + MongoDB as our Persistence Framework setup.
I am facing a problem with multiple requests for CRUD operations from the server. The Exception that I'm getting is as follows -
[Transaction is currently active] with root cause java.lang.IllegalStateException
The Entity Manager is initialized as follows :
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PersistenceUnitProperties.CLASSLOADER, DAOServiceImpl.class.getClassLoader());
entityManagerFactory = new PersistenceProvider().createEntityManagerFactory("mongo", properties);
entityManager = entityManagerFactory.createEntityManager();
public <T> T createEntity(final T entity) {
entityManager.getTransaction().begin();
entityManager.persist(entity);
entityManager.getTransaction().commit();
return entity;
}
public <T> T readEntity(final String entityId, final Class<T> entityClass) {
return entityManager.find(entityClass, entityId);
}
public <T> T updateEntity(final T entity) {
entityManager.getTransaction().begin();
T merged = entityManager.merge(entity);
entityManager.getTransaction().commit();
return merged;
}
public <T> boolean deleteEntity(final String entityId, final Class<T> entityClass) {
entityManager.getTransaction().begin();
T entity = entityManager.find(entityClass, entityId);
entityManager.remove(entity);
entityManager.getTransaction().commit();
return true;
}
The persistence.xml file is as follows :
<persistence-unit name="mongo" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>Model1</class>
..
<class>Modeln</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database"
value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform" />
<property name="eclipselink.nosql.connection-spec"
value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec" />
<property name="eclipselink.nosql.property.mongo.port"
value="27017" />
<property name="eclipselink.nosql.property.mongo.host"
value="localhost" />
<property name="eclipselink.nosql.property.mongo.db" value="esysdb" />
<!-- property name="eclipselink.logging.level" value="FINEST" / -->
</properties>
</persistence-unit>
</persistence>
We are using only one EntityManager in our entire application.
Am I doing the setup in a correct way?
I understand that EclipseLink + MongoDB does not support transactions. But, could anybody please let me know if there are any workarounds for this ?
Can EclipseLink + JPA + MongoDB support multi-user scenario with proper handling of database transacations ?
How can I prevent the usage of EntityManager by multiple requests at once ?
Thanks.