Red Hat Web Application Framework 6.1 Manual de usuario Pagina 105

  • Descarga
  • Añadir a mis manuales
  • Imprimir
  • Pagina
    / 230
  • Tabla de contenidos
  • MARCADORES
  • Valorado. / 5. Basado en revisión del cliente
Vista de pagina 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();
}
}
Vista de pagina 104
1 2 ... 100 101 102 103 104 105 106 107 108 109 110 ... 229 230

Comentarios a estos manuales

Sin comentarios