Search in sources :

Example 1 with SqlExceptionHelper

use of org.hibernate.engine.jdbc.spi.SqlExceptionHelper in project hibernate-orm by hibernate.

the class AbstractRegionAccessStrategyTest method mockedSession.

protected SharedSessionContractImplementor mockedSession() {
    SessionMock session = mock(SessionMock.class);
    when(session.isClosed()).thenReturn(false);
    when(session.getTimestamp()).thenReturn(TIME_SERVICE.wallClockTime());
    if (jtaPlatform == BatchModeJtaPlatform.class) {
        BatchModeTransactionCoordinator txCoord = new BatchModeTransactionCoordinator();
        when(session.getTransactionCoordinator()).thenReturn(txCoord);
        when(session.beginTransaction()).then(invocation -> {
            Transaction tx = txCoord.newTransaction();
            tx.begin();
            return tx;
        });
    } else if (jtaPlatform == null) {
        Connection connection = mock(Connection.class);
        JdbcConnectionAccess jdbcConnectionAccess = mock(JdbcConnectionAccess.class);
        try {
            when(jdbcConnectionAccess.obtainConnection()).thenReturn(connection);
        } catch (SQLException e) {
        // never thrown from mock
        }
        JdbcSessionOwner jdbcSessionOwner = mock(JdbcSessionOwner.class);
        when(jdbcSessionOwner.getJdbcConnectionAccess()).thenReturn(jdbcConnectionAccess);
        SqlExceptionHelper sqlExceptionHelper = mock(SqlExceptionHelper.class);
        JdbcServices jdbcServices = mock(JdbcServices.class);
        when(jdbcServices.getSqlExceptionHelper()).thenReturn(sqlExceptionHelper);
        ServiceRegistry serviceRegistry = mock(ServiceRegistry.class);
        when(serviceRegistry.getService(JdbcServices.class)).thenReturn(jdbcServices);
        JdbcSessionContext jdbcSessionContext = mock(JdbcSessionContext.class);
        when(jdbcSessionContext.getServiceRegistry()).thenReturn(serviceRegistry);
        when(jdbcSessionOwner.getJdbcSessionContext()).thenReturn(jdbcSessionContext);
        NonJtaTransactionCoordinator txOwner = mock(NonJtaTransactionCoordinator.class);
        when(txOwner.getResourceLocalTransaction()).thenReturn(new JdbcResourceTransactionMock());
        when(txOwner.getJdbcSessionOwner()).thenReturn(jdbcSessionOwner);
        when(txOwner.isActive()).thenReturn(true);
        TransactionCoordinator txCoord = JdbcResourceLocalTransactionCoordinatorBuilderImpl.INSTANCE.buildTransactionCoordinator(txOwner, null);
        when(session.getTransactionCoordinator()).thenReturn(txCoord);
        when(session.beginTransaction()).then(invocation -> {
            Transaction tx = new TransactionImpl(txCoord, session.getExceptionConverter());
            tx.begin();
            return tx;
        });
    } else {
        throw new IllegalStateException("Unknown JtaPlatform: " + jtaPlatform);
    }
    return session;
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) BatchModeJtaPlatform(org.hibernate.test.cache.infinispan.util.BatchModeJtaPlatform) Cache(org.infinispan.Cache) Transaction(org.hibernate.Transaction) AdvancedCache(org.infinispan.AdvancedCache) After(org.junit.After) Mockito.doAnswer(org.mockito.Mockito.doAnswer) CacheDataDescriptionImpl(org.hibernate.cache.internal.CacheDataDescriptionImpl) TestingUtil(org.infinispan.test.TestingUtil) AccessType(org.hibernate.cache.spi.access.AccessType) Predicate(java.util.function.Predicate) TombstoneUpdate(org.hibernate.cache.infinispan.util.TombstoneUpdate) JdbcResourceTransactionAccess(org.hibernate.resource.transaction.backend.jdbc.spi.JdbcResourceTransactionAccess) TestSynchronization(org.hibernate.test.cache.infinispan.util.TestSynchronization) Matchers.any(org.mockito.Matchers.any) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) SoftLock(org.hibernate.cache.spi.access.SoftLock) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) AfterClassOnce(org.hibernate.testing.AfterClassOnce) FutureUpdate(org.hibernate.cache.infinispan.util.FutureUpdate) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) PutFromLoadValidator(org.hibernate.cache.infinispan.access.PutFromLoadValidator) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Mockito.mock(org.mockito.Mockito.mock) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) TestResourceTracker(org.infinispan.test.fwk.TestResourceTracker) Logger(org.jboss.logging.Logger) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) Session(org.hibernate.Session) Caches(org.hibernate.cache.infinispan.util.Caches) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) RegionAccessStrategy(org.hibernate.cache.spi.access.RegionAccessStrategy) SQLException(java.sql.SQLException) TestTimeService(org.hibernate.test.cache.infinispan.util.TestTimeService) RollbackException(javax.transaction.RollbackException) TransactionCoordinatorOwner(org.hibernate.resource.transaction.spi.TransactionCoordinatorOwner) ExpectingInterceptor(org.hibernate.test.cache.infinispan.util.ExpectingInterceptor) ComparableComparator(org.hibernate.internal.util.compare.ComparableComparator) BatchModeTransactionCoordinator(org.hibernate.test.cache.infinispan.util.BatchModeTransactionCoordinator) JdbcResourceTransactionMock(org.hibernate.test.cache.infinispan.util.JdbcResourceTransactionMock) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AssertionFailedError(junit.framework.AssertionFailedError) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) Mockito.when(org.mockito.Mockito.when) BaseRegion(org.hibernate.cache.infinispan.impl.BaseRegion) ServiceRegistry(org.hibernate.service.ServiceRegistry) TimeUnit(java.util.concurrent.TimeUnit) CacheDataDescription(org.hibernate.cache.spi.CacheDataDescription) InvalidateCommand(org.infinispan.commands.write.InvalidateCommand) Assert.assertNull(org.junit.Assert.assertNull) SystemException(javax.transaction.SystemException) JdbcResourceLocalTransactionCoordinatorBuilderImpl(org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorBuilderImpl) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) BeforeClassOnce(org.hibernate.testing.BeforeClassOnce) Assert.assertEquals(org.junit.Assert.assertEquals) JdbcResourceTransactionMock(org.hibernate.test.cache.infinispan.util.JdbcResourceTransactionMock) JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) SQLException(java.sql.SQLException) Connection(java.sql.Connection) TransactionCoordinator(org.hibernate.resource.transaction.spi.TransactionCoordinator) BatchModeTransactionCoordinator(org.hibernate.test.cache.infinispan.util.BatchModeTransactionCoordinator) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) TransactionImpl(org.hibernate.engine.transaction.internal.TransactionImpl) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) BatchModeTransactionCoordinator(org.hibernate.test.cache.infinispan.util.BatchModeTransactionCoordinator) Transaction(org.hibernate.Transaction) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) ServiceRegistry(org.hibernate.service.ServiceRegistry)

Example 2 with SqlExceptionHelper

use of org.hibernate.engine.jdbc.spi.SqlExceptionHelper in project hibernate-orm by hibernate.

the class JdbcCoordinatorTest method testConnectionClose.

@Test
public void testConnectionClose() throws NoSuchFieldException, IllegalAccessException, SQLException {
    Connection connection = Mockito.mock(Connection.class);
    JdbcSessionOwner sessionOwner = Mockito.mock(JdbcSessionOwner.class);
    JdbcConnectionAccess jdbcConnectionAccess = Mockito.mock(JdbcConnectionAccess.class);
    when(jdbcConnectionAccess.obtainConnection()).thenReturn(connection);
    when(jdbcConnectionAccess.supportsAggressiveRelease()).thenReturn(false);
    JdbcSessionContext sessionContext = Mockito.mock(JdbcSessionContext.class);
    when(sessionOwner.getJdbcSessionContext()).thenReturn(sessionContext);
    when(sessionOwner.getJdbcConnectionAccess()).thenReturn(jdbcConnectionAccess);
    ServiceRegistry serviceRegistry = Mockito.mock(ServiceRegistry.class);
    when(sessionContext.getServiceRegistry()).thenReturn(serviceRegistry);
    when(sessionContext.getPhysicalConnectionHandlingMode()).thenReturn(PhysicalConnectionHandlingMode.IMMEDIATE_ACQUISITION_AND_HOLD);
    JdbcObserver jdbcObserver = Mockito.mock(JdbcObserver.class);
    when(sessionContext.getObserver()).thenReturn(jdbcObserver);
    JdbcServices jdbcServices = Mockito.mock(JdbcServices.class);
    when(serviceRegistry.getService(eq(JdbcServices.class))).thenReturn(jdbcServices);
    ConfigurationService configurationService = Mockito.mock(ConfigurationService.class);
    when(serviceRegistry.getService(eq(ConfigurationService.class))).thenReturn(configurationService);
    when(configurationService.getSetting(eq(AvailableSettings.CONNECTION_PROVIDER_DISABLES_AUTOCOMMIT), same(StandardConverters.BOOLEAN), eq(false))).thenReturn(false);
    SqlExceptionHelper sqlExceptionHelper = Mockito.mock(SqlExceptionHelper.class);
    when(jdbcServices.getSqlExceptionHelper()).thenReturn(sqlExceptionHelper);
    JdbcCoordinatorImpl jdbcCoordinator = new JdbcCoordinatorImpl(null, sessionOwner);
    Batch currentBatch = Mockito.mock(Batch.class);
    Field currentBatchField = JdbcCoordinatorImpl.class.getDeclaredField("currentBatch");
    currentBatchField.setAccessible(true);
    currentBatchField.set(jdbcCoordinator, currentBatch);
    doThrow(IllegalStateException.class).when(currentBatch).release();
    try {
        jdbcCoordinator.close();
        fail("Should throw IllegalStateException");
    } catch (Exception expected) {
        assertEquals(IllegalStateException.class, expected.getClass());
    }
    verify(jdbcConnectionAccess, times(1)).releaseConnection(same(connection));
}
Also used : JdbcSessionContext(org.hibernate.resource.jdbc.spi.JdbcSessionContext) Connection(java.sql.Connection) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper) JdbcSessionOwner(org.hibernate.resource.jdbc.spi.JdbcSessionOwner) SQLException(java.sql.SQLException) Field(java.lang.reflect.Field) JdbcObserver(org.hibernate.resource.jdbc.spi.JdbcObserver) Batch(org.hibernate.engine.jdbc.batch.spi.Batch) JdbcConnectionAccess(org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess) ServiceRegistry(org.hibernate.service.ServiceRegistry) ConfigurationService(org.hibernate.engine.config.spi.ConfigurationService) Test(org.junit.Test)

Example 3 with SqlExceptionHelper

use of org.hibernate.engine.jdbc.spi.SqlExceptionHelper in project hibernate-orm by hibernate.

the class JdbcEnvironmentImpl method buildSqlExceptionHelper.

@SuppressWarnings("deprecation")
private SqlExceptionHelper buildSqlExceptionHelper(Dialect dialect, boolean logWarnings) {
    final StandardSQLExceptionConverter sqlExceptionConverter = new StandardSQLExceptionConverter();
    sqlExceptionConverter.addDelegate(dialect.buildSQLExceptionConversionDelegate());
    sqlExceptionConverter.addDelegate(new SQLExceptionTypeDelegate(dialect));
    // todo : vary this based on extractedMetaDataSupport.getSqlStateType()
    sqlExceptionConverter.addDelegate(new SQLStateConversionDelegate(dialect));
    return new SqlExceptionHelper(sqlExceptionConverter, logWarnings);
}
Also used : StandardSQLExceptionConverter(org.hibernate.exception.internal.StandardSQLExceptionConverter) SQLStateConversionDelegate(org.hibernate.exception.internal.SQLStateConversionDelegate) SQLExceptionTypeDelegate(org.hibernate.exception.internal.SQLExceptionTypeDelegate) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper)

Example 4 with SqlExceptionHelper

use of org.hibernate.engine.jdbc.spi.SqlExceptionHelper in project uPortal by Jasig.

the class FixedDatabaseMetadata method getTableMetadata.

@Override
public TableMetadata getTableMetadata(String name, String schema, String catalog, boolean isQuoted) throws HibernateException {
    Object identifier = identifier(catalog, schema, name);
    TableMetadata table = (TableMetadata) tables.get(identifier);
    if (table != null) {
        return table;
    } else {
        try {
            ResultSet rs = null;
            try {
                if ((isQuoted && meta.storesMixedCaseQuotedIdentifiers())) {
                    rs = meta.getTables(catalog, schema, name, TYPES);
                } else if ((isQuoted && meta.storesUpperCaseQuotedIdentifiers()) || (!isQuoted && meta.storesUpperCaseIdentifiers())) {
                    rs = meta.getTables(StringHelper.toUpperCase(catalog), StringHelper.toUpperCase(schema), StringHelper.toUpperCase(name), TYPES);
                } else if ((isQuoted && meta.storesLowerCaseQuotedIdentifiers()) || (!isQuoted && meta.storesLowerCaseIdentifiers())) {
                    rs = meta.getTables(StringHelper.toLowerCase(catalog), StringHelper.toLowerCase(schema), StringHelper.toLowerCase(name), TYPES);
                } else {
                    rs = meta.getTables(catalog, schema, name, TYPES);
                }
                while (rs.next()) {
                    String tableName = rs.getString("TABLE_NAME");
                    if (name.equalsIgnoreCase(tableName)) {
                        table = new TableMetadata(rs, meta, extras);
                        tables.put(identifier, table);
                        return table;
                    }
                }
                LOG.tableNotFound(name);
                return null;
            } finally {
                if (rs != null)
                    rs.close();
            }
        } catch (SQLException sqlException) {
            throw new SqlExceptionHelper(sqlExceptionConverter).convert(sqlException, "could not get table metadata: " + name);
        }
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) SqlExceptionHelper(org.hibernate.engine.jdbc.spi.SqlExceptionHelper)

Aggregations

SqlExceptionHelper (org.hibernate.engine.jdbc.spi.SqlExceptionHelper)4 SQLException (java.sql.SQLException)3 Connection (java.sql.Connection)2 JdbcConnectionAccess (org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess)2 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)2 JdbcSessionContext (org.hibernate.resource.jdbc.spi.JdbcSessionContext)2 JdbcSessionOwner (org.hibernate.resource.jdbc.spi.JdbcSessionOwner)2 ServiceRegistry (org.hibernate.service.ServiceRegistry)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 Predicate (java.util.function.Predicate)1 RollbackException (javax.transaction.RollbackException)1 SystemException (javax.transaction.SystemException)1 AssertionFailedError (junit.framework.AssertionFailedError)1