當前位置: 首頁>>代碼示例>>Java>>正文


Java TransactionManager.commit方法代碼示例

本文整理匯總了Java中javax.transaction.TransactionManager.commit方法的典型用法代碼示例。如果您正苦於以下問題:Java TransactionManager.commit方法的具體用法?Java TransactionManager.commit怎麽用?Java TransactionManager.commit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.transaction.TransactionManager的用法示例。


在下文中一共展示了TransactionManager.commit方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: multipleCloseTest

import javax.transaction.TransactionManager; //導入方法依賴的package包/類
@Test
@DisplayName( "Multiple close test" )
public void multipleCloseTest() throws SQLException {
    TransactionManager txManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
    TransactionSynchronizationRegistry txSyncRegistry = new com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple();

    AgroalDataSourceConfigurationSupplier configurationSupplier = new AgroalDataSourceConfigurationSupplier()
            .connectionPoolConfiguration( cp -> cp
                    .transactionIntegration( new NarayanaTransactionIntegration( txManager, txSyncRegistry ) )
            );

    try ( AgroalDataSource dataSource = AgroalDataSource.from( configurationSupplier ) ) {

        // there is a call to connection#close in the try-with-resources block and another on the callback from the transaction#commit()
        try ( Connection connection = dataSource.getConnection() ) {
            logger.info( format( "Got connection {0}", connection ) );
            try {
                txManager.begin();
                txManager.commit();
            } catch ( NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e ) {
                fail( "Exception: " + e.getMessage() );
            }
        }  
    }
}
 
開發者ID:agroal,項目名稱:agroal,代碼行數:26,代碼來源:BasicNarayanaTests.java

示例2: enrollConnectionCloseTest

import javax.transaction.TransactionManager; //導入方法依賴的package包/類
@Test
@DisplayName( "Enroll connection after previous connection close test" )
public void enrollConnectionCloseTest() throws SQLException {
    TransactionManager txManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
    TransactionSynchronizationRegistry txSyncRegistry = new com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple();

    AgroalDataSourceConfigurationSupplier configurationSupplier = new AgroalDataSourceConfigurationSupplier()
            .connectionPoolConfiguration( cp -> cp
                    .transactionIntegration( new NarayanaTransactionIntegration( txManager, txSyncRegistry ) )
                    .connectionFactoryConfiguration( cf -> cf
                            .autoCommit( true ) )
            );

    try ( AgroalDataSource dataSource = AgroalDataSource.from( configurationSupplier ) ) {
        txManager.begin();

        Connection connection = dataSource.getConnection();
        logger.info( format( "Got connection {0}", connection ) );
        String connectionToString = connection.toString();
        connection.close();

        Connection secondConnection = dataSource.getConnection();
        logger.info( format( "Got connection {0}", secondConnection ) );

        // TODO: comparing toString is brittle. Find a better way to make sure the underlying physical connection is the same.
        assertEquals( connectionToString, secondConnection.toString(), "Expect the same connection under the same transaction" );
        assertFalse( secondConnection.getAutoCommit(), "AutoCommit temporarily disabled in enlisted connection" );
        secondConnection.close();

        txManager.commit();

        assertTrue( connection.isClosed() );
        assertTrue( secondConnection.isClosed() );
    } catch ( NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e ) {
        fail( "Exception: " + e.getMessage() );
    }
}
 
開發者ID:agroal,項目名稱:agroal,代碼行數:38,代碼來源:EnlistmentTests.java

示例3: lazyEnlistmentTest

import javax.transaction.TransactionManager; //導入方法依賴的package包/類
@Test
@DisplayName( "Lazy enlistment test" )
public void lazyEnlistmentTest() throws SQLException {
    TransactionManager txManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
    TransactionSynchronizationRegistry txSyncRegistry = new com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple();

    AgroalDataSourceConfigurationSupplier configurationSupplier = new AgroalDataSourceConfigurationSupplier()
            .connectionPoolConfiguration( cp -> cp
                    .transactionIntegration( new NarayanaTransactionIntegration( txManager, txSyncRegistry ) )
            );

    try ( AgroalDataSource dataSource = AgroalDataSource.from( configurationSupplier ) ) {
        Connection connection = dataSource.getConnection();
        logger.info( format( "Got connection {0}", connection ) );

        try {
            txManager.begin();
            assertThrows( SQLException.class, connection::createStatement );
            logger.info( format( "Call to a method on the connection thrown a SQLException" ) );
            txManager.commit();

            assertFalse( connection.isClosed(), "Not expecting the connection to be close since it was not enrolled into the transaction" );
        } catch ( NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e ) {
            fail( "Exception: " + e.getMessage() );
        }
    }
}
 
開發者ID:agroal,項目名稱:agroal,代碼行數:28,代碼來源:EnlistmentTests.java

示例4: basicConnectionAcquireTest

import javax.transaction.TransactionManager; //導入方法依賴的package包/類
@Test
@DisplayName( "Connection acquire test" )
public void basicConnectionAcquireTest() throws SQLException {
    TransactionManager txManager = com.arjuna.ats.jta.TransactionManager.transactionManager();
    TransactionSynchronizationRegistry txSyncRegistry = new com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple();

    AgroalDataSourceConfigurationSupplier configurationSupplier = new AgroalDataSourceConfigurationSupplier()
            .connectionPoolConfiguration( cp -> cp
                    .transactionIntegration( new NarayanaTransactionIntegration( txManager, txSyncRegistry ) )
                    .connectionFactoryConfiguration( cf -> cf.autoCommit( true ) )
            );

    try ( AgroalDataSource dataSource = AgroalDataSource.from( configurationSupplier ) ) {
        txManager.begin();

        Connection connection = dataSource.getConnection();
        logger.info( format( "Got connection {0}", connection ) );

        assertAll( () -> {
            assertThrows( SQLException.class, () -> connection.setAutoCommit( true ) );
            assertFalse( connection.getAutoCommit(), "Expect connection to have autocommit not set" );
            // TODO: comparing toString is brittle. Find a better way to make sure the underlying physical connection is the same.
            assertEquals( connection.toString(), dataSource.getConnection().toString(), "Expect the same connection under the same transaction" );
        } );

        txManager.commit();

        assertTrue( connection.isClosed() );
    } catch ( NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e ) {
        fail( "Exception: " + e.getMessage() );
    }
}
 
開發者ID:agroal,項目名稱:agroal,代碼行數:33,代碼來源:BasicNarayanaTests.java

示例5: main

import javax.transaction.TransactionManager; //導入方法依賴的package包/類
public static void main(String[] args) {

		TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();

		//build the EntityManagerFactory as you would build in in Hibernate Core
		EntityManagerFactory emf = Persistence.createEntityManagerFactory( "ogm-jpa-tutorial" );

		//Persist entities the way you are used to in plain JPA
		try {
			tm.begin();
			logger.infof( "About to store dog and breed" );
			EntityManager em = emf.createEntityManager();
			Breed collie = new Breed();
			collie.setName( "Collie" );
			em.persist( collie );
			Dog dina = new Dog();
			dina.setName( "Dina" );
			dina.setBreed( collie );
			em.persist( dina );
			Long dinaId = dina.getId();
			em.flush();
			em.close();
			tm.commit();

			//Retrieve your entities the way you are used to in plain JPA
			logger.infof( "About to retrieve dog and breed" );
			tm.begin();
			em = emf.createEntityManager();
			dina = em.find( Dog.class, dinaId );
			logger.infof( "Found dog %s of breed %s", dina.getName(), dina.getBreed().getName() );
			em.flush();
			em.close();
			tm.commit();

			emf.close();
		}
		catch ( Exception e ) {
			e.printStackTrace();
		}

	}
 
開發者ID:hibernate,項目名稱:hibernate-ogm-redis,代碼行數:42,代碼來源:DogBreedRunner.java


注:本文中的javax.transaction.TransactionManager.commit方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。