Red Hat Web Application Framework 6.1 Manuel d'utilisateur Page 105

  • Télécharger
  • Ajouter à mon manuel
  • Imprimer
  • Page
    / 230
  • Table des matières
  • MARQUE LIVRES
  • Noté. / 5. Basé sur avis des utilisateurs
Vue de la page 104
Chapter 9. Persistence Tutorial 91
9.7.2. Manual Transaction Management
When you manage your transactions locally, your starting point is the SessionManager class. From
there, you can get access to the Session class and then to the Transaction. Specifically, the Session
provides access to the TransactionContext, which has methods for beginning a transaction, committing
a transaction, and aborting a transaction. For example, the following code will open a transaction,
execute the necessary code and then either commit or abort the transaction:
import com.arsdigita.persistence.Session;
import com.arsdigita.persistence.SessionManager;
import com.arsdigita.persistence.TransactionContext;
Session session = SessionManager.getSession();
TransactionContext txn = session.getTransactionContext();
try {
txn.beginTxn();
// do some work here
txn.commitTxn();
} catch (Exception e) {
txn.abortTxn();
}
Transactions cannot be nested. If you have to make sure that you are in a transaction, use the inTxn()
method of TransactionContext, as in the following example:
import com.arsdigita.persistence.Session;
import com.arsdigita.persistence.SessionManager;
import com.arsdigita.persistence.TransactionContext;
Session session = SessionManager.getSession();
TransactionContext txn = session.getTransactionContext();
boolean openedTransaction = false;
try {
if (!txn.inTxn()) {
openedTransaction = true;
txn.beginTxn();
}
// do some work here
if (openedTransaction) {
txn.commitTxn();
}
} catch (Exception e) {
if (openedTransaction) {
txn.abortTxn();
}
}
Vue de la page 104
1 2 ... 100 101 102 103 104 105 106 107 108 109 110 ... 229 230

Commentaires sur ces manuels

Pas de commentaire