Search in sources :

Example 1 with SessionImplementor

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

the class TransactionTimeoutTest method testTransactionTimeoutFailure.

@Test
public void testTransactionTimeoutFailure() throws InterruptedException {
    Session session = openSession();
    try {
        Transaction transaction = session.getTransaction();
        transaction.setTimeout(1);
        assertEquals(-1, ((SessionImplementor) session).getJdbcCoordinator().determineRemainingTransactionTimeOutPeriod());
        transaction.begin();
        Thread.sleep(1000);
        session.persist(new Person("Lukasz", "Antoniak"));
        transaction.commit();
    } catch (TransactionException e) {
    // expected
    } catch (PersistenceException e) {
        assertTyping(TransactionException.class, e.getCause());
    } finally {
        session.close();
    }
}
Also used : TransactionException(org.hibernate.TransactionException) Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Person(org.hibernate.test.jdbc.Person) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with SessionImplementor

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

the class AuditProcessManager method get.

public AuditProcess get(EventSource session) {
    final Transaction transaction = session.accessTransaction();
    AuditProcess auditProcess = auditProcesses.get(transaction);
    if (auditProcess == null) {
        // No worries about registering a transaction twice - a transaction is single thread
        auditProcess = new AuditProcess(revisionInfoGenerator, session);
        auditProcesses.put(transaction, auditProcess);
        session.getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() {

            public void doBeforeTransactionCompletion(SessionImplementor session) {
                final AuditProcess process = auditProcesses.get(transaction);
                if (process != null) {
                    process.doBeforeTransactionCompletion(session);
                }
            }
        });
        session.getActionQueue().registerProcess(new AfterTransactionCompletionProcess() {

            public void doAfterTransactionCompletion(boolean success, SharedSessionContractImplementor session) {
                auditProcesses.remove(transaction);
            }
        });
    }
    return auditProcess;
}
Also used : BeforeTransactionCompletionProcess(org.hibernate.action.spi.BeforeTransactionCompletionProcess) Transaction(org.hibernate.Transaction) AfterTransactionCompletionProcess(org.hibernate.action.spi.AfterTransactionCompletionProcess) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor)

Example 3 with SessionImplementor

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

the class CrossTypeRevisionChangesReaderImpl method findEntityTypes.

@Override
@SuppressWarnings({ "unchecked" })
public Set<Pair<String, Class>> findEntityTypes(Number revision) throws IllegalStateException, IllegalArgumentException {
    checkNotNull(revision, "Entity revision");
    checkPositive(revision, "Entity revision");
    checkSession();
    final Session session = auditReaderImplementor.getSession();
    final SessionImplementor sessionImplementor = auditReaderImplementor.getSessionImplementor();
    final Set<Number> revisions = new HashSet<>(1);
    revisions.add(revision);
    final Query<?> query = enversService.getRevisionInfoQueryCreator().getRevisionsQuery(session, revisions);
    final Object revisionInfo = query.uniqueResult();
    if (revisionInfo != null) {
        // If revision exists.
        final Set<String> entityNames = enversService.getModifiedEntityNamesReader().getModifiedEntityNames(revisionInfo);
        if (entityNames != null) {
            // Generate result that contains entity names and corresponding Java classes.
            final Set<Pair<String, Class>> result = new HashSet<>();
            for (String entityName : entityNames) {
                result.add(Pair.make(entityName, EntityTools.getEntityClass(sessionImplementor, entityName)));
            }
            return result;
        }
    }
    return Collections.EMPTY_SET;
}
Also used : SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) HashSet(java.util.HashSet) Pair(org.hibernate.envers.tools.Pair)

Example 4 with SessionImplementor

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

the class SimpleAuditExpression method addToQuery.

@Override
protected void addToQuery(EnversService enversService, AuditReaderImplementor versionsReader, String entityName, String alias, QueryBuilder qb, Parameters parameters) {
    String propertyName = CriteriaTools.determinePropertyName(enversService, versionsReader, entityName, propertyNameGetter);
    RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(enversService, entityName, propertyName);
    if (relatedEntity == null) {
        // HHH-9178 - Add support to component type equality.
        // This basically will allow = and <> operators to perform component-based equality checks.
        // Any other operator for a component type will not be supported.
        // Non-component types will continue to behave normally.
        final SessionImplementor session = versionsReader.getSessionImplementor();
        final Type type = getPropertyType(session, entityName, propertyName);
        if (type != null && type.isComponentType()) {
            if (!"=".equals(op) && !"<>".equals(op)) {
                throw new AuditException("Component-based criterion is not supported for op: " + op);
            }
            final ComponentType componentType = (ComponentType) type;
            for (int i = 0; i < componentType.getPropertyNames().length; i++) {
                final Object componentValue = componentType.getPropertyValue(value, i, session);
                parameters.addWhereWithParam(alias, propertyName + "_" + componentType.getPropertyNames()[i], op, componentValue);
            }
        } else {
            parameters.addWhereWithParam(alias, propertyName, op, value);
        }
    } else {
        if (!"=".equals(op) && !"<>".equals(op)) {
            throw new AuditException("This type of operation: " + op + " (" + entityName + "." + propertyName + ") isn't supported and can't be used in queries.");
        }
        Object id = relatedEntity.getIdMapper().mapToIdFromEntity(value);
        relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, id, alias, null, "=".equals(op));
    }
}
Also used : ComponentType(org.hibernate.type.ComponentType) Type(org.hibernate.type.Type) ComponentType(org.hibernate.type.ComponentType) RelationDescription(org.hibernate.envers.internal.entities.RelationDescription) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) AuditException(org.hibernate.envers.exception.AuditException)

Example 5 with SessionImplementor

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

the class AutoDiscoveryTest method testDialectGetColumnAliasExtractor.

@Test
public void testDialectGetColumnAliasExtractor() throws Exception {
    Session session = openSession();
    final SessionImplementor sessionImplementor = (SessionImplementor) session;
    session.beginTransaction();
    session.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            PreparedStatement ps = sessionImplementor.getJdbcCoordinator().getStatementPreparer().prepareStatement(QUERY_STRING);
            ResultSet rs = sessionImplementor.getJdbcCoordinator().getResultSetReturn().extract(ps);
            try {
                ResultSetMetaData metadata = rs.getMetaData();
                String column1Alias = getDialect().getColumnAliasExtractor().extractColumnAlias(metadata, 1);
                String column2Alias = getDialect().getColumnAliasExtractor().extractColumnAlias(metadata, 2);
                Assert.assertFalse("bad dialect.getColumnAliasExtractor impl", column1Alias.equals(column2Alias));
            } finally {
                sessionImplementor.getJdbcCoordinator().getResourceRegistry().release(rs, ps);
                sessionImplementor.getJdbcCoordinator().getResourceRegistry().release(ps);
            }
        }
    });
    session.getTransaction().commit();
    session.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PreparedStatement(java.sql.PreparedStatement) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

SessionImplementor (org.hibernate.engine.spi.SessionImplementor)97 Test (org.junit.Test)60 Session (org.hibernate.Session)58 Connection (java.sql.Connection)20 TestForIssue (org.hibernate.testing.TestForIssue)18 PreparedStatement (java.sql.PreparedStatement)17 EntityPersister (org.hibernate.persister.entity.EntityPersister)14 Work (org.hibernate.jdbc.Work)13 Statement (java.sql.Statement)12 List (java.util.List)12 Transaction (org.hibernate.Transaction)12 ResultSet (java.sql.ResultSet)11 SQLException (java.sql.SQLException)11 Serializable (java.io.Serializable)8 ArrayList (java.util.ArrayList)7 EntityEntry (org.hibernate.engine.spi.EntityEntry)7 JDBCException (org.hibernate.JDBCException)6 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)6 QueryParameters (org.hibernate.engine.spi.QueryParameters)6 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)6