0

I am learning EJB3.1 and JPA2

I am doing following things for Insert and Update

INSERT

Common com = new Common();
com.setKeyData(keyData);
com.setKeyValue("0001");
em.persist(com);

UPDATE

Common com = em.find(Common.class, pk);
com.setKeyValue("0002");

The above code works but i would like to know if that's all i need to do for inserting and updating. I saw few posts where they do begin(), commit() etc (probably used in swing app). As i am new to JPA/ORM i really want to know if the above code is enough for a application with lot of data load, is there anything i am missing or should i read/learn more about inserting/updating data.

When should i use the EntityManager's close,clear and flush methods, in which scenario is it used.

user1433804
  • 657
  • 5
  • 17
  • 31

4 Answers4

1

Your code should be fine in most cases. In a lot of EJB development, the lifecycle of transactions will be handled by the container - so there is no need to explicitly mark the beginning of a transaction.

For more fine-grained control you can also use Bean managed transactions, in which you will need to call begin and commit/rollback to manage the lifecycle yourself.

For more detail take a look at:

Container managed: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Transaction3.html Bean managed: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Transaction4.html

jcern
  • 7,798
  • 4
  • 39
  • 47
1

When using EJB with JPA , you need not worry after begining a transaction , commiting them. All that is done by the underlying Container . When using EJB , you just need to annotate it with @PersistentContext(name="CustDB")

@PersistenceContext(name="PersistentUnitName")
private EntityManager entityManager;
Durgadas Kamath
  • 400
  • 2
  • 12
1

begin/commit are used for controlling transactions (to ensure ACID) if this isn't done by something else for you (see other posts).

A possible use of a transaction can look like this:

EntityManager em = ...
EntityTransaction tx = null;
try {
  tx = em.getTransaction();
  tx.begin();
  // your code that should be executed within the transaction
  tx.commit();
} catch(Exception ex) {
  // roll back if something failed
  if (tx != null && tx.isActive()) {
    tx.rollback();
  }
} finally {
  // make sure em is closed on commit and on rollback
  em.close();
}
micha
  • 47,774
  • 16
  • 73
  • 80
0

You should close EntityManager if it is application managed. It means that you created it by yourself (without injection). You should use begin,commit ... if you want to manage your transaction by youself (BMT or Bean Managed Transaction). But usually you will work with CMT(Container Managed Transaction) and this methods will not be useful. You can use flush anywhere if transaction is currently opened. You can use clear method if you want to detach all manged entities from persistence context.

But my answers are very short and I think that this link will be useful for you. There are a lot of details in related books too.

gkuzmin
  • 2,414
  • 17
  • 24