- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
public Object save(Object entity) {
final EntityManager em = getEntityManager();
try {
final EntityTransaction et = em.getTransaction();
try {
et.begin();
em.persist(entity);
et.commit();
} finally {
if (et != null && et.isActive()) {
entity = null;
et.rollback();
}
}
} finally {
if (em != null && em.isOpen()) {
em.close();
}
}
return entity;
}
guest 23.09.2009 16:29 # 0
rh.mbox 23.09.2009 17:23 # 0
Ну обычно делают что-то вроде:
public Object save(Object entity) {
final EntityManager em = getEntityManager();
final EntityTransaction transaction = em.getTransaction();
try {
transaction.begin();
em.persist(entity);
transaction.commit();
} catch (RuntimeException e) {
transaction.rollback();
throw e;
} finally {
if (em.isOpen()) {
em.close();
}
}
return entity;
}
rh.mbox 23.09.2009 17:24 # 0
guest 23.09.2009 17:37 # 0
guest 23.09.2009 17:40 # 0
rh.mbox 23.09.2009 18:45 # +3
guest 24.09.2009 08:20 # −1
guest 25.09.2009 10:45 # +1