下面列出了怎么用org.hibernate.TransactionException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* shared logic for tearing down resources used in our unit tests
*/
public static void tearDownHelper() {
TransactionException rollbackException = null;
if (HibernateFactory.inTransaction()) {
try {
HibernateFactory.rollbackTransaction();
//HibernateFactory.commitTransaction();
}
catch (TransactionException e) {
rollbackException = e;
}
}
HibernateFactory.closeSession();
if (rollbackException != null) {
throw rollbackException;
}
// In case someone disabled it and forgot to
// renable it.
RhnBaseTestCase.enableLocalizationServiceLogging();
}
@Override
public void begin() {
try {
if ( getStatus() == TransactionStatus.NOT_ACTIVE ) {
log.trace( "Calling TransactionManager#begin" );
transactionManager.begin();
initiator = true;
log.trace( "Called TransactionManager#begin" );
}
else {
log.trace( "Skipping TransactionManager#begin due to already active transaction" );
}
}
catch (Exception e) {
throw new TransactionException( "JTA TransactionManager#begin failed", e );
}
}
@Override
public void commit() {
try {
if ( initiator ) {
initiator = false;
log.trace( "Calling TransactionManager#commit" );
transactionManager.commit();
log.trace( "Called TransactionManager#commit" );
}
else {
log.trace( "Skipping TransactionManager#commit due to not being initiator" );
}
}
catch (Exception e) {
throw new TransactionException( "JTA TransactionManager#commit failed", e );
}
}
@Override
public void rollback() {
try {
if ( initiator ) {
initiator = false;
log.trace( "Calling TransactionManager#rollback" );
transactionManager.rollback();
log.trace( "Called TransactionManager#rollback" );
}
else {
markRollbackOnly();
}
}
catch (Exception e) {
throw new TransactionException( "JTA TransactionManager#rollback failed", e );
}
}
@Override
public void begin() {
try {
if ( getStatus() == TransactionStatus.NOT_ACTIVE ) {
log.trace( "Calling UserTransaction#begin" );
userTransaction.begin();
initiator = true;
log.trace( "Called UserTransaction#begin" );
}
else {
log.trace( "Skipping TransactionManager#begin due to already active transaction" );
}
}
catch (Exception e) {
throw new TransactionException( "JTA UserTransaction#begin failed", e );
}
}
@Override
public void commit() {
try {
if ( initiator ) {
initiator = false;
log.trace( "Calling UserTransaction#commit" );
userTransaction.commit();
log.trace( "Called UserTransaction#commit" );
}
else {
log.trace( "Skipping TransactionManager#commit due to not being initiator" );
}
}
catch (Exception e) {
throw new TransactionException( "JTA UserTransaction#commit failed", e );
}
}
@Override
public void rollback() {
try {
if ( initiator ) {
initiator = false;
log.trace( "Calling UserTransaction#rollback" );
userTransaction.rollback();
log.trace( "Called UserTransaction#rollback" );
}
else {
markRollbackOnly();
}
}
catch (Exception e) {
throw new TransactionException( "JTA UserTransaction#rollback failed", e );
}
}
public void begin() throws HibernateException {
if (begun) {
return;
}
log.debug("begin");
boolean synchronization = jdbcContext.registerSynchronizationIfPossible();
if ( !synchronization ) {
throw new TransactionException("Could not register synchronization for container transaction");
}
begun = true;
jdbcContext.afterTransactionBegin(this);
}
public void commit() throws HibernateException {
if (!begun) {
throw new TransactionException("Transaction not successfully started");
}
log.debug("commit");
boolean flush = !transactionContext.isFlushModeNever() &&
!transactionContext.isFlushBeforeCompletionEnabled();
if (flush) {
transactionContext.managedFlush(); //if an exception occurs during flush, user must call rollback()
}
begun = false;
}
public void rollback() throws HibernateException {
if (!begun) {
throw new TransactionException("Transaction not successfully started");
}
log.debug("rollback");
try {
getTransaction().setRollbackOnly();
}
catch (SystemException se) {
log.error("Could not set transaction to rollback only", se);
throw new TransactionException("Could not set transaction to rollback only", se);
}
begun = false;
}
public boolean isActive() throws TransactionException {
if (!begun) return false;
final int status;
try {
status = getTransaction().getStatus();
}
catch (SystemException se) {
log.error("Could not determine transaction status", se);
throw new TransactionException("Could not determine transaction status: ", se);
}
if (status==Status.STATUS_UNKNOWN) {
throw new TransactionException("Could not determine transaction status");
}
else {
return status==Status.STATUS_ACTIVE;
}
}
public boolean wasRolledBack() throws TransactionException {
if (!begun) return false;
final int status;
try {
status = getTransaction().getStatus();
}
catch (SystemException se) {
log.error("Could not determine transaction status", se);
throw new TransactionException("Could not determine transaction status", se);
}
if (status==Status.STATUS_UNKNOWN) {
throw new TransactionException("Could not determine transaction status");
}
else {
return JTAHelper.isRollback(status);
}
}
public boolean wasCommitted() throws TransactionException {
if ( !begun ) return false;
final int status;
try {
status = getTransaction().getStatus();
}
catch (SystemException se) {
log.error("Could not determine transaction status", se);
throw new TransactionException("Could not determine transaction status: ", se);
}
if (status==Status.STATUS_UNKNOWN) {
throw new TransactionException("Could not determine transaction status");
}
else {
return status==Status.STATUS_COMMITTED;
}
}
public JTATransaction(
InitialContext context,
String utName,
JDBCContext jdbcContext,
TransactionFactory.Context transactionContext
) {
this.jdbcContext = jdbcContext;
this.transactionContext = transactionContext;
log.debug("Looking for UserTransaction under: " + utName);
try {
ut = (UserTransaction) context.lookup(utName);
}
catch (NamingException ne) {
log.error("Could not find UserTransaction in JNDI", ne);
throw new TransactionException("Could not find UserTransaction in JNDI: ", ne);
}
if (ut==null) {
throw new AssertionFailure("A naming service lookup returned null");
}
log.debug("Obtained UserTransaction");
}
public boolean wasRolledBack() throws TransactionException {
//if (!begun) return false;
//if (commitFailed) return true;
final int status;
try {
status = ut.getStatus();
}
catch (SystemException se) {
log.error("Could not determine transaction status", se);
throw new TransactionException("Could not determine transaction status", se);
}
if (status==Status.STATUS_UNKNOWN) {
throw new TransactionException("Could not determine transaction status");
}
else {
return JTAHelper.isRollback(status);
}
}
public boolean wasCommitted() throws TransactionException {
//if (!begun || commitFailed) return false;
final int status;
try {
status = ut.getStatus();
}
catch (SystemException se) {
log.error("Could not determine transaction status", se);
throw new TransactionException("Could not determine transaction status: ", se);
}
if (status==Status.STATUS_UNKNOWN) {
throw new TransactionException("Could not determine transaction status");
}
else {
return status==Status.STATUS_COMMITTED;
}
}
public boolean isActive() throws TransactionException {
if (!begun || commitFailed || commitSucceeded) return false;
final int status;
try {
status = ut.getStatus();
}
catch (SystemException se) {
log.error("Could not determine transaction status", se);
throw new TransactionException("Could not determine transaction status: ", se);
}
if (status==Status.STATUS_UNKNOWN) {
throw new TransactionException("Could not determine transaction status");
}
else {
return status==Status.STATUS_ACTIVE;
}
}
/**
* shared logic for tearing down resources used in our unit tests
*/
public static void tearDownHelper() {
TransactionException rollbackException = null;
if (HibernateFactory.inTransaction()) {
try {
HibernateFactory.rollbackTransaction();
//HibernateFactory.commitTransaction();
}
catch (TransactionException e) {
rollbackException = e;
}
}
HibernateFactory.closeSession();
if (rollbackException != null) {
throw rollbackException;
}
// In case someone disabled it and forgot to
// renable it.
RhnBaseTestCase.enableLocalizationServiceLogging();
}
@Test
public void givenTxnMarkedRollbackOnly_whenCommitted_thenTransactionException() {
thrown.expect(isA(TransactionException.class));
Session session = null;
Transaction transaction = null;
try {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
Product product1 = new Product();
product1.setId(15);
product1.setName("Product1");
session.save(product1);
transaction.setRollbackOnly();
transaction.commit();
} catch (Exception e) {
rollbackTransactionQuietly(transaction);
throw (e);
} finally {
closeSessionQuietly(session);
}
}
@Override
public boolean canRegisterSynchronization() {
try {
return (this.transactionManager.getStatus() == Status.STATUS_ACTIVE);
}
catch (SystemException ex) {
throw new TransactionException("Could not determine JTA transaction status", ex);
}
}
@Override
public void registerSynchronization(Synchronization synchronization) {
if (this.transactionSynchronizationRegistry != null) {
this.transactionSynchronizationRegistry.registerInterposedSynchronization(synchronization);
}
else {
try {
this.transactionManager.getTransaction().registerSynchronization(synchronization);
}
catch (Exception ex) {
throw new TransactionException("Could not access JTA Transaction to register synchronization", ex);
}
}
}
@Override
public boolean canRegisterSynchronization() {
try {
return (this.transactionManager.getStatus() == Status.STATUS_ACTIVE);
}
catch (SystemException ex) {
throw new TransactionException("Could not determine JTA transaction status", ex);
}
}
@Override
public void registerSynchronization(Synchronization synchronization) {
if (this.transactionSynchronizationRegistry != null) {
this.transactionSynchronizationRegistry.registerInterposedSynchronization(synchronization);
}
else {
try {
this.transactionManager.getTransaction().registerSynchronization(synchronization);
}
catch (Exception ex) {
throw new TransactionException("Could not access JTA Transaction to register synchronization", ex);
}
}
}
public boolean canRegisterSynchronization() {
try {
return (this.transactionManager.getStatus() == Status.STATUS_ACTIVE);
}
catch (SystemException ex) {
throw new TransactionException("Could not determine JTA transaction status", ex);
}
}
public void registerSynchronization(Synchronization synchronization) {
if (this.transactionSynchronizationRegistry != null) {
this.transactionSynchronizationRegistry.registerInterposedSynchronization(synchronization);
}
else {
try {
this.transactionManager.getTransaction().registerSynchronization(synchronization);
}
catch (Exception ex) {
throw new TransactionException("Could not access JTA Transaction to register synchronization", ex);
}
}
}
@Override
public boolean canRegisterSynchronization() {
try {
return (this.transactionManager.getStatus() == Status.STATUS_ACTIVE);
}
catch (SystemException ex) {
throw new TransactionException("Could not determine JTA transaction status", ex);
}
}
@Override
public void registerSynchronization(Synchronization synchronization) {
if (this.transactionSynchronizationRegistry != null) {
this.transactionSynchronizationRegistry.registerInterposedSynchronization(synchronization);
}
else {
try {
this.transactionManager.getTransaction().registerSynchronization(synchronization);
}
catch (Exception ex) {
throw new TransactionException("Could not access JTA Transaction to register synchronization", ex);
}
}
}
@Override
public TransactionStatus getStatus() {
try {
return StatusTranslator.translate( transactionManager.getStatus() );
}
catch (SystemException e) {
throw new TransactionException( "JTA TransactionManager#getStatus failed", e );
}
}
@Override
public void markRollbackOnly() {
try {
transactionManager.setRollbackOnly();
}
catch (SystemException e) {
throw new TransactionException( "Could not set transaction to rollback only", e );
}
}
@Override
public TransactionStatus getStatus() {
try {
return StatusTranslator.translate( userTransaction.getStatus() );
}
catch (SystemException e) {
throw new TransactionException( "JTA TransactionManager#getStatus failed", e );
}
}