Beispiel für BeanManagedTransactions und Hibernate gesucht

sepan

Grünschnabel
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:
@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?
 

Neue Beiträge

Zurück