Java Code Examples for org.springframework.jdbc.datasource.DataSourceUtils#prepareConnectionForTransaction()

The following examples show how to use org.springframework.jdbc.datasource.DataSourceUtils#prepareConnectionForTransaction() . 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: HibernateJpaDialect.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
		throws PersistenceException, SQLException, TransactionException {

	Session session = getSession(entityManager);

	if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
		session.getTransaction().setTimeout(definition.getTimeout());
	}

	boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT);
	Integer previousIsolationLevel = null;
	Connection preparedCon = null;

	if (isolationLevelNeeded || definition.isReadOnly()) {
		if (this.prepareConnection) {
			preparedCon = HibernateConnectionHandle.doGetConnection(session);
			previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(preparedCon, definition);
		}
		else if (isolationLevelNeeded) {
			throw new InvalidIsolationLevelException(getClass().getSimpleName() +
					" does not support custom isolation levels since the 'prepareConnection' flag is off. " +
					"This is the case on Hibernate 3.6 by default; either switch that flag at your own risk " +
					"or upgrade to Hibernate 4.x, with 4.2+ recommended.");
		}
	}

	// Standard JPA transaction begin call for full JPA context setup...
	entityManager.getTransaction().begin();

	// Adapt flush mode and store previous isolation level, if any.
	FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly());
	return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel);
}
 
Example 2
Source File: HibernateJpaDialect.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
		throws PersistenceException, SQLException, TransactionException {

	Session session = getSession(entityManager);

	if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
		session.getTransaction().setTimeout(definition.getTimeout());
	}

	boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT);
	Integer previousIsolationLevel = null;
	Connection preparedCon = null;

	if (isolationLevelNeeded || definition.isReadOnly()) {
		if (this.prepareConnection) {
			preparedCon = HibernateConnectionHandle.doGetConnection(session);
			previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(preparedCon, definition);
		}
		else if (isolationLevelNeeded) {
			throw new InvalidIsolationLevelException(getClass().getSimpleName() +
					" does not support custom isolation levels since the 'prepareConnection' flag is off. " +
					"This is the case on Hibernate 3.6 by default; either switch that flag at your own risk " +
					"or upgrade to Hibernate 4.x, with 4.2+ recommended.");
		}
	}

	// Standard JPA transaction begin call for full JPA context setup...
	entityManager.getTransaction().begin();

	// Adapt flush mode and store previous isolation level, if any.
	FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly());
	return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel);
}
 
Example 3
Source File: HibernateJpaDialect.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
		throws PersistenceException, SQLException, TransactionException {

	Session session = getSession(entityManager);

	if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
		session.getTransaction().setTimeout(definition.getTimeout());
	}

	boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT);
	Integer previousIsolationLevel = null;
	Connection preparedCon = null;

	if (isolationLevelNeeded || definition.isReadOnly()) {
		if (this.prepareConnection) {
			preparedCon = HibernateConnectionHandle.doGetConnection(session);
			previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(preparedCon, definition);
		}
		else if (isolationLevelNeeded) {
			throw new InvalidIsolationLevelException(getClass().getSimpleName() +
					" does not support custom isolation levels since the 'prepareConnection' flag is off.");
		}
	}

	// Standard JPA transaction begin call for full JPA context setup...
	entityManager.getTransaction().begin();

	// Adapt flush mode and store previous isolation level, if any.
	FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly());
	if (definition instanceof ResourceTransactionDefinition &&
			((ResourceTransactionDefinition) definition).isLocalResource()) {
		// As of 5.1, we explicitly optimize for a transaction-local EntityManager,
		// aligned with native HibernateTransactionManager behavior.
		previousFlushMode = null;
		if (definition.isReadOnly()) {
			session.setDefaultReadOnly(true);
		}
	}
	return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel);
}
 
Example 4
Source File: HibernateJpaDialect.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition)
		throws PersistenceException, SQLException, TransactionException {

	Session session = getSession(entityManager);

	if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) {
		session.getTransaction().setTimeout(definition.getTimeout());
	}

	boolean isolationLevelNeeded = (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT);
	Integer previousIsolationLevel = null;
	Connection preparedCon = null;

	if (isolationLevelNeeded || definition.isReadOnly()) {
		if (this.prepareConnection) {
			preparedCon = HibernateConnectionHandle.doGetConnection(session);
			previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(preparedCon, definition);
		}
		else if (isolationLevelNeeded) {
			throw new InvalidIsolationLevelException(getClass().getSimpleName() +
					" does not support custom isolation levels since the 'prepareConnection' flag is off.");
		}
	}

	// Standard JPA transaction begin call for full JPA context setup...
	entityManager.getTransaction().begin();

	// Adapt flush mode and store previous isolation level, if any.
	FlushMode previousFlushMode = prepareFlushMode(session, definition.isReadOnly());
	if (definition instanceof ResourceTransactionDefinition &&
			((ResourceTransactionDefinition) definition).isLocalResource()) {
		// As of 5.1, we explicitly optimize for a transaction-local EntityManager,
		// aligned with native HibernateTransactionManager behavior.
		previousFlushMode = null;
		if (definition.isReadOnly()) {
			session.setDefaultReadOnly(true);
		}
	}
	return new SessionTransactionData(session, previousFlushMode, preparedCon, previousIsolationLevel);
}