ERLEDIGT
NEIN
NEIN
ANTWORTEN
0
0
ZUGRIFFE
451
451
EMPFEHLEN
-
Hallo,
ich suche ein Beispiel für Bean Managed Transactions mit Hibernate unter JBoss 4.2.2 (vorzugsweise mit EJB3). Verwendet werden sollten Hibernate-Transactions (also session.beginTransaction()) und im Beispiel mindestens 2 voneinander unabhängige Transaktionen gestartet und committed werden.
Folgender Code von mir funktioniert nicht (kein Update in DB). Führt man den Code von modifyEntity auf dem Client aus, wird ein Update durchgeführt.
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
@Stateless @Remote @TransactionManagement(TransactionManagementType.BEAN) public class BMTTestBean implements BMTTest { static Logger log = LogManager.getLogger(BMTTestBean.class); @PersistenceContext EntityManager em; @Resource UserTransaction ut; @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void doTransactionTest() { log.info("Start of transaction test ---------------------------------"); try { log.debug("Beginning new UserTransaction"); ut.begin(); Session session = (Session)em.getDelegate(); modifyEntity(session, "a98bbda7-b78f-40db-820d-2fa912f1b956"); modifyEntity(session, "a1c53abe-ea0a-46dc-8f17-9726af8ba034"); // folgender Eintrag existiert nicht modifyEntity(session, "0915bfcc-ffff-4c27-a7e7-647396f56bad"); log.debug("Committing UserTransaction"); } catch (Exception ex) { log.error("Rolling back UserTransaction", ex); try { ut.setRollbackOnly(); } catch (Exception ex2) { log.error("Failed to mark UserTransaction as rolled back", ex2); } } finally { try { if (ut.getStatus() == Status.STATUS_ACTIVE) { try { ut.commit(); } catch (Exception ex) { log.error("Failed to commit UserTransaction", ex); } } else { try { ut.rollback(); } catch (Exception ex) { log.error("Failed to rollback UserTransaction", ex); } } } catch (SystemException ex) { log.error("Unable to determine status of UserTransaction", ex); } } log.info("End of transaction test -----------------------------------"); } private void modifyEntity(Session session, String id) { log.info("modifyEntity : beginTransaction"); Transaction tx = session.beginTransaction(); try { MyEntity e = (MyEntity)session.load(MyEntity.class, id); log.info(String.format("Loaded entity %s with text: %s", e.getId(), e.getText())); e.setModifiedTimestamp(new Date()); log.info("Updating entity"); session.update(e); log.info("modifyEntity : commit"); tx.commit(); } catch (HibernateException ex) { log.error("Failed to load entity, rolling back transaction", ex); try { tx.rollback(); } catch (HibernateException ex2) { log.error("Failed to rollback transaction", ex2); } } } }
Kann mir jemand weiterhelfen?
Ähnliche Themen
-
Kleines Beispiel zu JPA mit Spring 3.x, Hibernate 3.x, Lombok ,JUnit 4 und Maven 2.x
Von Thomas Darimont im Forum JavaAntworten: 10Letzter Beitrag: 05.03.12, 09:23 -
Beispiel für Springframework / Hibernate / Equinox / OSGi / Eclipse / ExtensionPoints
Von Thomas Darimont im Forum JavaAntworten: 5Letzter Beitrag: 06.11.08, 19:21 -
explode Beispiel gesucht
Von BastianW im Forum JavaAntworten: 5Letzter Beitrag: 05.03.07, 16:03 -
Hibernate Mapping Beispiel?
Von Romsl im Forum JavaAntworten: 7Letzter Beitrag: 18.05.05, 21:58





Zitieren
Login





