ObjectDB ObjectDB

Attempt to begin a new transaction when a transaction is active

#1

The code

I have a RecordDao stateless java bean : 


@Stateless
public class RecordDao {

private EntityManagerFactory emf;
private EntityManager em;

public RecordDao() {
}


@PostConstruct
public void init() {
  emf = Persistence.createEntityManagerFactory("recordPU");
  em = emf.createEntityManager();
  System.out.println("RecordDao Initialized.");
}


@PreDestroy
public void release() {
  em.close();
  emf.close();
}


public void storeRecord(Record r) {
  em.getTransaction().begin();
  em.persist(r);
  em.getTransaction().commit();
  System.out.println("Entity persisted");
}

}

 

Persistence unit looks like this :

<persistence-unit name="recordPU" transaction-type="JTA">
    <provider>com.objectdb.jpa.Provider</provider>
    <properties>
      <property name="javax.persistence.jdbc.url" value="$objectdb/db/recs.odb"/>
      <property name="javax.persistence.jdbc.user" value="admin"/>
      <property name="javax.persistence.jdbc.password" value="admin"/>
    </properties>
  </persistence-unit>

 

From other beans I need concurrent access to the database in order to persist records. 

For example :


@EJB    RecordDao recordDao;

recordDao.storeRecord(updatedRecord);

or

recordDao.storeRecord(newRecord);

Other concurrent operations that I execute is to retrieve records from database using :

Record r = em.find(Record.class, recordNo);

The issue (error is obtained at line em.persist(r); in storeRecord(Record r) function, RecordDAO class)

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: com.objectdb.o._PersistenceException: Attempt to begin a new transaction when a transaction is active
root cause

com.objectdb.o._PersistenceException: Attempt to begin a new transaction when a transaction is active
root cause

com.objectdb.o.UserException: Attempt to begin a new transaction when a transaction is active

 

I don't know why am I getting this..

Thank you very much!

edit
delete
#2

Apparently the transaction is started automatically by the application server, and as the error message states, you are trying to the start a transaction that is already active. Try commenting the begin and commit commands.

Note that usually when you use JPA with a Java EE application server, you do not instantiate the EntityManagerFactory and the EntityManager explicitly, but let the application server do it for you (see this tutorial).

ObjectDB Support
edit
delete
#3

Thank you very very much! 

edit
delete

Reply

To post on this website please sign in.