Java Code Examples for org.hibernate.resource.transaction.spi.TransactionStatus#MARKED_ROLLBACK

The following examples show how to use org.hibernate.resource.transaction.spi.TransactionStatus#MARKED_ROLLBACK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransactionsTest.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void jdbc() {
	//tag::transactions-api-jdbc-example[]
	StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
			// "jdbc" is the default, but for explicitness
			.applySetting( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jdbc" )
			.build();

	Metadata metadata = new MetadataSources( serviceRegistry )
			.addAnnotatedClass( Customer.class )
			.getMetadataBuilder()
			.build();

	SessionFactory sessionFactory = metadata.getSessionFactoryBuilder()
			.build();

	Session session = sessionFactory.openSession();
	try {
		// calls Connection#setAutoCommit( false ) to
		// signal start of transaction
		session.getTransaction().begin();

		session.createQuery( "UPDATE customer set NAME = 'Sir. '||NAME" )
				.executeUpdate();

		// calls Connection#commit(), if an error
		// happens we attempt a rollback
		session.getTransaction().commit();
	}
	catch ( Exception e ) {
		// we may need to rollback depending on
		// where the exception happened
		if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE
				|| session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK ) {
			session.getTransaction().rollback();
		}
		// handle the underlying error
	}
	finally {
		session.close();
		sessionFactory.close();
	}
	//end::transactions-api-jdbc-example[]
}
 
Example 2
Source File: StatusTranslator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static TransactionStatus translate(int status) {
	TransactionStatus transactionStatus = null;
	switch ( status ) {
		case Status.STATUS_ACTIVE:
			transactionStatus = TransactionStatus.ACTIVE;
			break;
		case Status.STATUS_PREPARED:
			transactionStatus = TransactionStatus.ACTIVE;
			break;
		case Status.STATUS_PREPARING:
			transactionStatus = TransactionStatus.ACTIVE;
			break;
		case Status.STATUS_COMMITTING:
			transactionStatus = TransactionStatus.COMMITTING;
			break;
		case Status.STATUS_ROLLING_BACK:
			transactionStatus = TransactionStatus.ROLLING_BACK;
			break;
		case Status.STATUS_NO_TRANSACTION:
			transactionStatus = TransactionStatus.NOT_ACTIVE;
			break;
		case Status.STATUS_COMMITTED:
			transactionStatus = TransactionStatus.COMMITTED;
			break;
		case Status.STATUS_ROLLEDBACK:
			transactionStatus = TransactionStatus.ROLLED_BACK;
			break;
		case Status.STATUS_MARKED_ROLLBACK:
			transactionStatus = TransactionStatus.MARKED_ROLLBACK;
			break;
		default:
			break;
	}
	if ( transactionStatus == null ) {
		throw new TransactionException( "TransactionManager reported transaction status as unknwon" );
	}
	return transactionStatus;
}
 
Example 3
Source File: TransactionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean getRollbackOnly() {
	if ( !isActive() ) {
		if ( jpaCompliance.isJpaTransactionComplianceEnabled() ) {
			throw new IllegalStateException(
					"JPA compliance dictates throwing IllegalStateException when #getRollbackOnly " +
							"is called on non-active transaction"
			);
		}
	}

	return getStatus() == TransactionStatus.MARKED_ROLLBACK;
}
 
Example 4
Source File: TransactionsTest.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void cmt() {
	//tag::transactions-api-cmt-example[]
	StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
			// "jdbc" is the default, but for explicitness
			.applySetting( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" )
			.build();

	Metadata metadata = new MetadataSources( serviceRegistry )
			.addAnnotatedClass( Customer.class )
			.getMetadataBuilder()
			.build();

	SessionFactory sessionFactory = metadata.getSessionFactoryBuilder()
			.build();

	// Note: depending on the JtaPlatform used and some optional settings,
	// the underlying transactions here will be controlled through either
	// the JTA TransactionManager or UserTransaction

	Session session = sessionFactory.openSession();
	try {
		// Since we are in CMT, a JTA transaction would
		// already have been started.  This call essentially
		// no-ops
		session.getTransaction().begin();

		Number customerCount = (Number) session.createQuery( "select count(c) from Customer c" ).uniqueResult();

		// Since we did not start the transaction ( CMT ),
		// we also will not end it.  This call essentially
		// no-ops in terms of transaction handling.
		session.getTransaction().commit();
	}
	catch ( Exception e ) {
		// again, the rollback call here would no-op (aside from
		// marking the underlying CMT transaction for rollback only).
		if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE
				|| session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK ) {
			session.getTransaction().rollback();
		}
		// handle the underlying error
	}
	finally {
		session.close();
		sessionFactory.close();
	}
	//end::transactions-api-cmt-example[]
}
 
Example 5
Source File: TransactionsTest.java    From google-cloud-spanner-hibernate with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void bmt() {
	//tag::transactions-api-bmt-example[]
	StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
			// "jdbc" is the default, but for explicitness
			.applySetting( AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta" )
			.build();

	Metadata metadata = new MetadataSources( serviceRegistry )
			.addAnnotatedClass( Customer.class )
			.getMetadataBuilder()
			.build();

	SessionFactory sessionFactory = metadata.getSessionFactoryBuilder()
			.build();

	// Note: depending on the JtaPlatform used and some optional settings,
	// the underlying transactions here will be controlled through either
	// the JTA TransactionManager or UserTransaction

	Session session = sessionFactory.openSession();
	try {
		// Assuming a JTA transaction is not already active,
		// this call the TM/UT begin method.  If a JTA
		// transaction is already active, we remember that
		// the Transaction associated with the Session did
		// not "initiate" the JTA transaction and will later
		// nop-op the commit and rollback calls...
		session.getTransaction().begin();

		session.persist( new Customer(  ) );
		Customer customer = (Customer) session.createQuery( "select c from Customer c" ).uniqueResult();

		// calls TM/UT commit method, assuming we are initiator.
		session.getTransaction().commit();
	}
	catch ( Exception e ) {
		// we may need to rollback depending on
		// where the exception happened
		if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE
				|| session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK ) {
			// calls TM/UT commit method, assuming we are initiator;
			// otherwise marks the JTA transaction for rollback only
			session.getTransaction().rollback();
		}
		// handle the underlying error
	}
	finally {
		session.close();
		sessionFactory.close();
	}
	//end::transactions-api-bmt-example[]
}
 
Example 6
Source File: JdbcResourceLocalTransactionCoordinatorImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public TransactionStatus getStatus() {
	return rollbackOnly ? TransactionStatus.MARKED_ROLLBACK : jdbcResourceTransaction.getStatus();
}