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();
}
}
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;
}
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;
}
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));
}
}
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();
}
Aggregations