org.hibernate.engine.jdbc.spi.JdbcServices Java Examples

The following examples show how to use org.hibernate.engine.jdbc.spi.JdbcServices. 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: InlineIdsSubSelectValuesListDeleteHandlerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public InlineIdsSubSelectValuesListDeleteHandlerImpl(
		SessionFactoryImplementor factory,
		HqlSqlWalker walker) {
	super( factory, walker );

	Dialect dialect = factory().getServiceRegistry().getService( JdbcServices.class ).getDialect();
	if ( !dialect.supportsRowValueConstructorSyntaxInInList() ) {
		throw new UnsupportedOperationException(
				"The " + getClass().getSimpleName() +
						" can only be used with Dialects that support IN clause row-value expressions (for composite identifiers)!"
		);
	}
	if ( !dialect.supportsValuesList() ) {
		throw new UnsupportedOperationException(
				"The " + getClass().getSimpleName() +
						" can only be used with Dialects that support VALUES lists!"
		);
	}
}
 
Example #2
Source File: AbstractLoadPlanBasedLoader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private ResultSet wrapResultSetIfEnabled(final ResultSet rs, final SharedSessionContractImplementor session) {
	if ( session.getFactory().getSessionFactoryOptions().isWrapResultSetsEnabled() ) {
		try {
			if ( log.isDebugEnabled() ) {
				log.debugf( "Wrapping result set [%s]", rs );
			}
			ResultSetWrapper wrapper = session.getFactory()
					.getServiceRegistry()
					.getService( JdbcServices.class )
					.getResultSetWrapper();
			// synchronized to avoid multi-thread access issues
			// Apparently the comment about this needing synchronization was introduced when AbstractLoadPlanBasedLoader first appeared
			// in version control. Would need to investigate if it's still needed?
			synchronized ( this ) {
				return wrapper.wrap( rs, retreiveColumnNameToIndexCache( rs ) );
			}
		}
		catch(SQLException e) {
			log.unableToWrapResultSet( e );
			return rs;
		}
	}
	else {
		return rs;
	}
}
 
Example #3
Source File: Loader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Modify the SQL, adding lock hints and comments, if necessary
 */
protected String preprocessSQL(
		String sql,
		QueryParameters parameters,
		SessionFactoryImplementor sessionFactory,
		List<AfterLoadAction> afterLoadActions) throws HibernateException {

	Dialect dialect = sessionFactory.getServiceRegistry().getService( JdbcServices.class ).getDialect();

	sql = applyLocks( sql, parameters, dialect, afterLoadActions );

	sql = dialect.addSqlHintOrComment(
		sql,
		parameters,
		sessionFactory.getSessionFactoryOptions().isCommentsEnabled()
	);

	return processDistinctKeyword( sql, parameters );
}
 
Example #4
Source File: Loader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private ResultSet wrapResultSetIfEnabled(final ResultSet rs, final SharedSessionContractImplementor session) {
	if ( session.getFactory().getSessionFactoryOptions().isWrapResultSetsEnabled() ) {
		try {
			LOG.debugf( "Wrapping result set [%s]", rs );
			return session.getFactory()
					.getServiceRegistry()
					.getService( JdbcServices.class )
					.getResultSetWrapper().wrap( rs, retrieveColumnNameToIndexCache( rs ) );
		}
		catch (SQLException e) {
			LOG.unableToWrapResultSet( e );
			return rs;
		}
	}
	else {
		return rs;
	}
}
 
Example #5
Source File: TypeSafeActivator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked", "UnusedParameters"})
private static void applyRelationalConstraints(ValidatorFactory factory, ActivationContext activationContext) {
	final ConfigurationService cfgService = activationContext.getServiceRegistry().getService( ConfigurationService.class );
	if ( !cfgService.getSetting( BeanValidationIntegrator.APPLY_CONSTRAINTS, StandardConverters.BOOLEAN, true  ) ) {
		LOG.debug( "Skipping application of relational constraints from legacy Hibernate Validator" );
		return;
	}

	final Set<ValidationMode> modes = activationContext.getValidationModes();
	if ( ! ( modes.contains( ValidationMode.DDL ) || modes.contains( ValidationMode.AUTO ) ) ) {
		return;
	}

	applyRelationalConstraints(
			factory,
			activationContext.getMetadata().getEntityBindings(),
			cfgService.getSettings(),
			activationContext.getServiceRegistry().getService( JdbcServices.class ).getDialect(),
			new ClassLoaderAccessImpl(
					null,
					activationContext.getServiceRegistry().getService( ClassLoaderService.class )
			)
	);
}
 
Example #6
Source File: JdbcCoordinatorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs a JdbcCoordinatorImpl
 *
 * @param userSuppliedConnection The user supplied connection (may be null)
 */
public JdbcCoordinatorImpl(
		Connection userSuppliedConnection,
		JdbcSessionOwner owner) {
	this.isUserSuppliedConnection = userSuppliedConnection != null;

	final ResourceRegistry resourceRegistry = new ResourceRegistryStandardImpl(
			owner.getJdbcSessionContext().getObserver()
	);
	if ( isUserSuppliedConnection ) {
		this.logicalConnection = new LogicalConnectionProvidedImpl( userSuppliedConnection, resourceRegistry );
	}
	else {
		this.logicalConnection = new LogicalConnectionManagedImpl(
				owner.getJdbcConnectionAccess(),
				owner.getJdbcSessionContext(),
				resourceRegistry
		);
	}
	this.owner = owner;
	this.exceptionHelper = owner.getJdbcSessionContext()
			.getServiceRegistry()
			.getService( JdbcServices.class )
			.getSqlExceptionHelper();
}
 
Example #7
Source File: AbstractBatchImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected AbstractBatchImpl(BatchKey key, JdbcCoordinator jdbcCoordinator) {
	if ( key == null ) {
		throw new IllegalArgumentException( "batch key cannot be null" );
	}
	if ( jdbcCoordinator == null ) {
		throw new IllegalArgumentException( "JDBC coordinator cannot be null" );
	}
	this.key = key;
	this.jdbcCoordinator = jdbcCoordinator;

	final JdbcServices jdbcServices = jdbcCoordinator.getJdbcSessionOwner()
			.getJdbcSessionContext()
			.getServiceRegistry()
			.getService( JdbcServices.class );

	this.sqlStatementLogger = jdbcServices.getSqlStatementLogger();
	this.sqlExceptionHelper = jdbcServices.getSqlExceptionHelper();
}
 
Example #8
Source File: PersistentTableBulkIdStrategy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void finishPreparation(
		JdbcServices jdbcServices,
		JdbcConnectionAccess connectionAccess,
		MetadataImplementor metadata,
		PreparationContextImpl context) {
	IdTableHelper.INSTANCE.executeIdTableCreationStatements(
			context.creationStatements,
			jdbcServices,
			connectionAccess
	);

	this.dropTableStatements = dropIdTables
			? context.dropStatements.toArray( new String[ context.dropStatements.size() ] )
			: null;
}
 
Example #9
Source File: PersistentTableBulkIdStrategy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected IdTableInfoImpl buildIdTableInfo(
		PersistentClass entityBinding,
		Table idTable,
		JdbcServices jdbcServices,
		MetadataImplementor metadata,
		PreparationContextImpl context) {
	final String renderedName = jdbcServices.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
			idTable.getQualifiedTableName(),
			jdbcServices.getJdbcEnvironment().getDialect()
	);

	context.creationStatements.add( buildIdTableCreateStatement( idTable, jdbcServices, metadata ) );
	if ( dropIdTables ) {
		context.dropStatements.add( buildIdTableDropStatement( idTable, jdbcServices ) );
	}

	return new IdTableInfoImpl( renderedName );
}
 
Example #10
Source File: Dialect.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Blob mergeBlob(Blob original, Blob target, SharedSessionContractImplementor session) {
	if ( original == null && target == null ) {
		return null;
	}
	try {
		final LobCreator lobCreator = session.getFactory().getServiceRegistry().getService( JdbcServices.class ).getLobCreator(
				session
		);
		return original == null
				? lobCreator.createBlob( ArrayHelper.EMPTY_BYTE_ARRAY )
				: lobCreator.createBlob( original.getBinaryStream(), original.length() );
	}
	catch (SQLException e) {
		throw session.getFactory().getSQLExceptionHelper().convert( e, "unable to merge BLOB data" );
	}
}
 
Example #11
Source File: LocalTemporaryTableBulkIdStrategy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected IdTableInfoImpl buildIdTableInfo(
		PersistentClass entityBinding,
		Table idTable,
		JdbcServices jdbcServices,
		MetadataImplementor metadata,
		PreparationContext context) {
	return new IdTableInfoImpl(
			jdbcServices.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
					idTable.getQualifiedTableName(),
					jdbcServices.getJdbcEnvironment().getDialect()
			),
			buildIdTableCreateStatement( idTable, jdbcServices, metadata ),
			buildIdTableDropStatement( idTable, jdbcServices )
	);
}
 
Example #12
Source File: Helper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void execute(Connection connection) {
	try {
		Statement statement = connection.createStatement();
		try {
			statement.executeUpdate( logStatement(factory, idTableInfo.getIdTableCreationStatement()) );
			factory.getServiceRegistry()
					.getService( JdbcServices.class )
					.getSqlExceptionHelper()
					.handleAndClearWarnings( statement, WARNING_HANDLER );
		}
		finally {
			try {
				statement.close();
			}
			catch( Throwable ignore ) {
				// ignore
			}
		}
	}
	catch( Exception e ) {
		log.debug( "unable to create temporary id table [" + e.getMessage() + "]" );
	}
}
 
Example #13
Source File: Helper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void execute(Connection connection) {
	try {
		Statement statement = connection.createStatement();
		try {
			statement.executeUpdate( logStatement( factory, idTableInfo.getIdTableDropStatement() ) );
			factory.getServiceRegistry()
					.getService( JdbcServices.class )
					.getSqlExceptionHelper()
					.handleAndClearWarnings( statement, WARNING_HANDLER );
		}
		finally {
			try {
				statement.close();
			}
			catch( Throwable ignore ) {
				// ignore
			}
		}
	}
	catch( Exception e ) {
		log.warn( "unable to drop temporary id table after use [" + e.getMessage() + "]" );
	}
}
 
Example #14
Source File: GlobalTemporaryTableBulkIdStrategy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected IdTableInfoImpl buildIdTableInfo(
		PersistentClass entityBinding,
		Table idTable,
		JdbcServices jdbcServices,
		MetadataImplementor metadata,
		PreparationContextImpl context) {
	context.creationStatements.add( buildIdTableCreateStatement( idTable, jdbcServices, metadata ) );
	if ( dropIdTables ) {
		context.dropStatements.add( buildIdTableDropStatement( idTable, jdbcServices ) );
	}

	final String renderedName = jdbcServices.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
			idTable.getQualifiedTableName(),
			jdbcServices.getJdbcEnvironment().getDialect()
	);

	return new IdTableInfoImpl( renderedName );
}
 
Example #15
Source File: InlineIdsSubSelectValuesListUpdateHandlerImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public InlineIdsSubSelectValuesListUpdateHandlerImpl(
		SessionFactoryImplementor factory,
		HqlSqlWalker walker) {
	super( factory, walker );

	Dialect dialect = factory().getServiceRegistry().getService( JdbcServices.class ).getDialect();
	if ( !dialect.supportsRowValueConstructorSyntaxInInList() ) {
		throw new UnsupportedOperationException(
				"The " + getClass().getSimpleName() +
						" can only be used with Dialects that support IN clause row-value expressions (for composite identifiers)!"
		);
	}
	if ( !dialect.supportsValuesList() ) {
		throw new UnsupportedOperationException(
				"The " + getClass().getSimpleName() +
						" can only be used with Dialects that support VALUES lists!"
		);
	}
}
 
Example #16
Source File: InlineIdsInClauseBulkIdStrategy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void prepare(
		JdbcServices jdbcServices,
		JdbcConnectionAccess jdbcConnectionAccess,
		MetadataImplementor metadataImplementor,
		SessionFactoryOptions sessionFactoryOptions) {
	// nothing to do
}
 
Example #17
Source File: GlobalTemporaryTableBulkIdStrategy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void release(
		JdbcServices jdbcServices,
		JdbcConnectionAccess connectionAccess) {
	if ( ! dropIdTables ) {
		return;
	}

	IdTableHelper.INSTANCE.executeIdTableDropStatements( dropTableStatements, jdbcServices, connectionAccess );
}
 
Example #18
Source File: InlineIdsOrClauseUpdateHandlerImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Update generateUpdate(
		String tableName,
		String[] columnNames,
		String idSubselect,
		String comment) {
	final Update update = new Update( factory().getServiceRegistry().getService( JdbcServices.class ).getDialect() )
			.setTableName( tableName )
			.setWhere( idSubselect );
	if ( factory().getSessionFactoryOptions().isCommentsEnabled() ) {
		update.setComment( comment );
	}
	return update;
}
 
Example #19
Source File: SimpleValueBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Dialect getDialect() {
	return buildingContext.getBuildingOptions()
			.getServiceRegistry()
			.getService( JdbcServices.class )
			.getJdbcEnvironment()
			.getDialect();
}
 
Example #20
Source File: InlineIdsInClauseUpdateHandlerImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public InlineIdsInClauseUpdateHandlerImpl(
		SessionFactoryImplementor factory,
		HqlSqlWalker walker) {
	super( factory, walker );
	Dialect dialect = factory.getServiceRegistry().getService( JdbcServices.class ).getDialect();
	if ( !dialect.supportsRowValueConstructorSyntaxInInList() ) {
		throw new UnsupportedOperationException(
				"The " + getClass().getSimpleName() +
						" can only be used with Dialects that support IN clause row-value expressions (for composite identifiers)!"
		);
	}
}
 
Example #21
Source File: InlineIdsIdsInClauseDeleteHandlerImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public InlineIdsIdsInClauseDeleteHandlerImpl(
		SessionFactoryImplementor factory,
		HqlSqlWalker walker) {
	super( factory, walker );
	Dialect dialect = factory.getServiceRegistry().getService( JdbcServices.class ).getDialect();
	if ( !dialect.supportsRowValueConstructorSyntaxInInList() ) {
		throw new UnsupportedOperationException(
				"The " + getClass().getSimpleName() +
						" can only be used with Dialects that support IN clause row-value expressions (for composite identifiers)!"
		);
	}
}
 
Example #22
Source File: AbstractCteValuesListBulkIdHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public AbstractCteValuesListBulkIdHandler(
		SessionFactoryImplementor sessionFactory, HqlSqlWalker walker,
		String catalog, String schema) {
	super( sessionFactory, walker );
	Dialect dialect = sessionFactory.getServiceRegistry().getService( JdbcServices.class ).getDialect();
	if ( !dialect.supportsNonQueryWithCTE() ) {
		throw new UnsupportedOperationException(
				"The " + getClass().getSimpleName() +
						" can only be used with Dialects that support CTE that can take UPDATE or DELETE statements as well!"
		);
	}
	if ( !dialect.supportsValuesList() ) {
		throw new UnsupportedOperationException(
				"The " + getClass().getSimpleName() +
						" can only be used with Dialects that support VALUES lists!"
		);
	}
	if ( !dialect.supportsRowValueConstructorSyntaxInInList() ) {
		throw new UnsupportedOperationException(
				"The " + getClass().getSimpleName() +
						" can only be used with Dialects that support IN clause row-value expressions (for composite identifiers)!"
		);
	}

	this.jdbcEnvironment = sessionFactory.getServiceRegistry().getService(
			JdbcServices.class ).getJdbcEnvironment();
	this.catalog = catalog;
	this.schema = schema;
}
 
Example #23
Source File: CteValuesListUpdateHandlerImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public CteValuesListUpdateHandlerImpl(
		SessionFactoryImplementor factory,
		HqlSqlWalker walker,
		String catalog,
		String schema) {
	super( factory, walker, catalog, schema );

	String[] tableNames = getTargetedQueryable().getConstraintOrderedTableNameClosure();
	String[][] columnNames = getTargetedQueryable().getContraintOrderedTableKeyColumnClosure();
	String idSubselect = generateIdSubselect( getTargetedQueryable() );

	updates = new String[tableNames.length];
	assignmentParameterSpecifications = new ParameterSpecification[tableNames.length][];
	for ( int tableIndex = 0; tableIndex < tableNames.length; tableIndex++ ) {
		boolean affected = false;
		final List<ParameterSpecification> parameterList = new ArrayList<>();
		final Update update = new Update( factory.getServiceRegistry().getService( JdbcServices.class ).getDialect() )
				.setTableName( tableNames[tableIndex] )
				.setWhere( "(" + String.join( ", ", (CharSequence[]) columnNames[tableIndex] ) + ") in (" + idSubselect + ")" );
		if ( factory().getSessionFactoryOptions().isCommentsEnabled() ) {
			update.setComment( "bulk update" );
		}
		final List<AssignmentSpecification> assignmentSpecifications = walker.getAssignmentSpecifications();
		for ( AssignmentSpecification assignmentSpecification : assignmentSpecifications ) {
			if ( assignmentSpecification.affectsTable( tableNames[tableIndex] ) ) {
				affected = true;
				update.appendAssignmentFragment( assignmentSpecification.getSqlAssignmentFragment() );
				if ( assignmentSpecification.getParameters() != null ) {
					Collections.addAll( parameterList, assignmentSpecification.getParameters() );
				}
			}
		}
		if ( affected ) {
			updates[tableIndex] = update.toStatementString();
			assignmentParameterSpecifications[tableIndex] = parameterList.toArray( new ParameterSpecification[parameterList.size()] );
		}
	}
}
 
Example #24
Source File: Helper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static String logStatement(SessionFactoryImplementor factory, String sql) {
	final SqlStatementLogger statementLogger = factory.getServiceRegistry()
			.getService( JdbcServices.class )
			.getSqlStatementLogger();

	statementLogger.logStatement( sql, FormatStyle.BASIC.getFormatter() );
	return sql;
}
 
Example #25
Source File: JtaTransactionCoordinatorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public IsolationDelegate createIsolationDelegate() {
	final JdbcSessionOwner jdbcSessionOwner = transactionCoordinatorOwner.getJdbcSessionOwner();

	return new JtaIsolationDelegate(
			jdbcSessionOwner.getJdbcConnectionAccess(),
			jdbcSessionOwner.getJdbcSessionContext()
					.getServiceRegistry()
					.getService( JdbcServices.class )
					.getSqlExceptionHelper(),
			jtaPlatform.retrieveTransactionManager()
	);
}
 
Example #26
Source File: LogicalConnectionManagedImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public LogicalConnectionManagedImpl(
		JdbcConnectionAccess jdbcConnectionAccess,
		JdbcSessionContext jdbcSessionContext,
		ResourceRegistry resourceRegistry) {
	this.jdbcConnectionAccess = jdbcConnectionAccess;
	this.observer = jdbcSessionContext.getObserver();
	this.resourceRegistry = resourceRegistry;

	this.connectionHandlingMode = determineConnectionHandlingMode(
			jdbcSessionContext.getPhysicalConnectionHandlingMode(),
			jdbcConnectionAccess

	);

	this.sqlExceptionHelper = jdbcSessionContext.getServiceRegistry()
			.getService( JdbcServices.class )
			.getSqlExceptionHelper();

	if ( connectionHandlingMode.getAcquisitionMode() == ConnectionAcquisitionMode.IMMEDIATELY ) {
		acquireConnectionIfNeeded();
	}

	this.providerDisablesAutoCommit = jdbcSessionContext.doesConnectionProviderDisableAutoCommit();
	if ( providerDisablesAutoCommit ) {
		log.debug(
				"`hibernate.connection.provider_disables_autocommit` was enabled.  This setting should only be " +
						"enabled when you are certain that the Connections given to Hibernate by the " +
						"ConnectionProvider have auto-commit disabled.  Enabling this setting when the " +
						"Connections do not have auto-commit disabled will lead to Hibernate executing " +
						"SQL operations outside of any JDBC/SQL transaction."
		);
	}
}
 
Example #27
Source File: Database.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static Dialect determineDialect(MetadataBuildingOptions buildingOptions) {
	final Dialect dialect = buildingOptions.getServiceRegistry().getService( JdbcServices.class ).getDialect();
	if ( dialect != null ) {
		return dialect;
	}

	// Use H2 dialect as default
	return new H2Dialect();
}
 
Example #28
Source File: Dialect.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public NClob mergeNClob(NClob original, NClob target, SharedSessionContractImplementor session) {
	if ( original == null && target == null ) {
		return null;
	}
	try {
		final LobCreator lobCreator = session.getFactory().getServiceRegistry().getService( JdbcServices.class ).getLobCreator( session );
		return original == null
				? lobCreator.createNClob( "" )
				: lobCreator.createNClob( original.getCharacterStream(), original.length() );
	}
	catch (SQLException e) {
		throw session.getFactory().getSQLExceptionHelper().convert( e, "unable to merge NCLOB data" );
	}
}
 
Example #29
Source File: Dialect.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Clob mergeClob(Clob original, Clob target, SharedSessionContractImplementor session) {
	if ( original == null && target == null ) {
		return null;
	}
	try {
		final LobCreator lobCreator = session.getFactory().getServiceRegistry().getService( JdbcServices.class ).getLobCreator( session );
		return original == null
				? lobCreator.createClob( "" )
				: lobCreator.createClob( original.getCharacterStream(), original.length() );
	}
	catch (SQLException e) {
		throw session.getFactory().getSQLExceptionHelper().convert( e, "unable to merge CLOB data" );
	}
}
 
Example #30
Source File: InlineIdsOrClauseBulkIdStrategy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void prepare(
		JdbcServices jdbcServices,
		JdbcConnectionAccess jdbcConnectionAccess,
		MetadataImplementor metadataImplementor,
		SessionFactoryOptions sessionFactoryOptions) {
	// nothing to do
}