org.springframework.jdbc.support.SQLExceptionTranslator Java Examples

The following examples show how to use org.springframework.jdbc.support.SQLExceptionTranslator. 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: SpringSessionSynchronization.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public SpringSessionSynchronization(
		SessionHolder sessionHolder, SessionFactory sessionFactory,
		SQLExceptionTranslator jdbcExceptionTranslator, boolean newSession) {

	this.sessionHolder = sessionHolder;
	this.sessionFactory = sessionFactory;
	this.jdbcExceptionTranslator = jdbcExceptionTranslator;
	this.newSession = newSession;

	// Check whether the SessionFactory has a JTA TransactionManager.
	TransactionManager jtaTm =
			SessionFactoryUtils.getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession());
	if (jtaTm != null) {
		this.hibernateTransactionCompletion = true;
		// Fetch current JTA Transaction object
		// (just necessary for JTA transaction suspension, with an individual
		// Hibernate Session per currently active/suspended transaction).
		try {
			this.jtaTransaction = jtaTm.getTransaction();
		}
		catch (SystemException ex) {
			throw new DataAccessResourceFailureException("Could not access JTA transaction", ex);
		}
	}
}
 
Example #2
Source File: SpringSessionSynchronization.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public SpringSessionSynchronization(
		SessionHolder sessionHolder, SessionFactory sessionFactory,
		SQLExceptionTranslator jdbcExceptionTranslator, boolean newSession) {

	this.sessionHolder = sessionHolder;
	this.sessionFactory = sessionFactory;
	this.jdbcExceptionTranslator = jdbcExceptionTranslator;
	this.newSession = newSession;

	// Check whether the SessionFactory has a JTA TransactionManager.
	TransactionManager jtaTm =
			SessionFactoryUtils.getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession());
	if (jtaTm != null) {
		this.hibernateTransactionCompletion = true;
		// Fetch current JTA Transaction object
		// (just necessary for JTA transaction suspension, with an individual
		// Hibernate Session per currently active/suspended transaction).
		try {
			this.jtaTransaction = jtaTm.getTransaction();
		}
		catch (SystemException ex) {
			throw new DataAccessResourceFailureException("Could not access JTA transaction", ex);
		}
	}
}
 
Example #3
Source File: HibernateAccessor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtain a default SQLExceptionTranslator, lazily creating it if necessary.
 * <p>Creates a default
 * {@link org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator}
 * for the SessionFactory's underlying DataSource.
 */
protected synchronized SQLExceptionTranslator getDefaultJdbcExceptionTranslator() {
	if (this.defaultJdbcExceptionTranslator == null) {
		this.defaultJdbcExceptionTranslator = SessionFactoryUtils.newJdbcExceptionTranslator(getSessionFactory());
	}
	return this.defaultJdbcExceptionTranslator;
}
 
Example #4
Source File: HibernateAccessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain a default SQLExceptionTranslator, lazily creating it if necessary.
 * <p>Creates a default
 * {@link org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator}
 * for the SessionFactory's underlying DataSource.
 */
protected synchronized SQLExceptionTranslator getDefaultJdbcExceptionTranslator() {
	if (this.defaultJdbcExceptionTranslator == null) {
		this.defaultJdbcExceptionTranslator = SessionFactoryUtils.newJdbcExceptionTranslator(getSessionFactory());
	}
	return this.defaultJdbcExceptionTranslator;
}
 
Example #5
Source File: HibernateTransactionManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtain a default SQLExceptionTranslator, lazily creating it if necessary.
 * <p>Creates a default
 * {@link org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator}
 * for the SessionFactory's underlying DataSource.
 */
protected synchronized SQLExceptionTranslator getDefaultJdbcExceptionTranslator() {
	if (this.defaultJdbcExceptionTranslator == null) {
		if (getDataSource() != null) {
			this.defaultJdbcExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(getDataSource());
		}
		else {
			this.defaultJdbcExceptionTranslator = SessionFactoryUtils.newJdbcExceptionTranslator(getSessionFactory());
		}
	}
	return this.defaultJdbcExceptionTranslator;
}
 
Example #6
Source File: HibernateTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain a default SQLExceptionTranslator, lazily creating it if necessary.
 * <p>Creates a default
 * {@link org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator}
 * for the SessionFactory's underlying DataSource.
 */
protected synchronized SQLExceptionTranslator getDefaultJdbcExceptionTranslator() {
	if (this.defaultJdbcExceptionTranslator == null) {
		if (getDataSource() != null) {
			this.defaultJdbcExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(getDataSource());
		}
		else {
			this.defaultJdbcExceptionTranslator = SessionFactoryUtils.newJdbcExceptionTranslator(getSessionFactory());
		}
	}
	return this.defaultJdbcExceptionTranslator;
}
 
Example #7
Source File: StoredProcedureTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public StoredProcedureExceptionTranslator(DataSource ds) {
	setDataSource(ds);
	setSql(SQL);
	getJdbcTemplate().setExceptionTranslator(new SQLExceptionTranslator() {

		@Override
		public DataAccessException translate(String task, String sql,
				SQLException sqlex) {
			return new CustomDataException(sql, sqlex);
		}

	});
	compile();
}
 
Example #8
Source File: GrailsHibernateTemplate.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("SqlDialectInspection")
protected DataAccessException convertJdbcAccessException(JDBCException ex, SQLExceptionTranslator translator) {
    String msg = ex.getMessage();
    String sql = ex.getSQL();
    SQLException sqlException = ex.getSQLException();
    return translator.translate("Hibernate operation: " + msg, sql, sqlException);
}
 
Example #9
Source File: StoredProcedureTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
public StoredProcedureExceptionTranslator(DataSource ds) {
	setDataSource(ds);
	setSql(SQL);
	getJdbcTemplate().setExceptionTranslator(new SQLExceptionTranslator() {
		@Override
		public DataAccessException translate(String task, @Nullable String sql, SQLException ex) {
			return new CustomDataException(sql, ex);
		}
	});
	compile();
}
 
Example #10
Source File: ExceptionTranslator.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public void exception(ExecuteContext context) {
    SQLDialect dialect = context.configuration().dialect();
    SQLExceptionTranslator translator = new SQLErrorCodeSQLExceptionTranslator(dialect.thirdParty().springDbName());

    context.exception(translator.translate("Access database using jOOQ", context.sql(), context.sqlException()));
}
 
Example #11
Source File: StoredProcedureTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
public StoredProcedureExceptionTranslator(DataSource ds) {
	setDataSource(ds);
	setSql(SQL);
	getJdbcTemplate().setExceptionTranslator(new SQLExceptionTranslator() {
		@Override
		public DataAccessException translate(String task, @Nullable String sql, SQLException ex) {
			return new CustomDataException(sql, ex);
		}
	});
	compile();
}
 
Example #12
Source File: StoredProcedureTests.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
public StoredProcedureExceptionTranslator(DataSource ds) {
	setDataSource(ds);
	setSql(SQL);
	getJdbcTemplate().setExceptionTranslator(new SQLExceptionTranslator() {

		@Override
		public DataAccessException translate(String task, String sql,
				SQLException sqlex) {
			return new CustomDataException(sql, sqlex);
		}

	});
	compile();
}
 
Example #13
Source File: JooqExceptionTranslator.java    From micronaut-sql with Apache License 2.0 5 votes vote down vote up
private SQLExceptionTranslator getTranslator(ExecuteContext context) {
    SQLDialect dialect = context.configuration().dialect();
    if (dialect != null && dialect.thirdParty() != null) {
        String dbName = dialect.thirdParty().springDbName();
        if (dbName != null) {
            return new SQLErrorCodeSQLExceptionTranslator(dbName);
        }
    }
    return new SQLStateSQLExceptionTranslator();
}
 
Example #14
Source File: SessionFactoryUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Register a JTA synchronization for the given Session, if any.
 * @param sessionHolder the existing thread-bound SessionHolder, if any
 * @param session the Session to register
 * @param sessionFactory the SessionFactory that the Session was created with
 * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the
 * Session on transaction synchronization (may be {@code null})
 */
private static void registerJtaSynchronization(Session session, SessionFactory sessionFactory,
		SQLExceptionTranslator jdbcExceptionTranslator, SessionHolder sessionHolder) {

	// JTA synchronization is only possible with a javax.transaction.TransactionManager.
	// We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified
	// in Hibernate configuration, it will contain a TransactionManager reference.
	TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, session);
	if (jtaTm != null) {
		try {
			Transaction jtaTx = jtaTm.getTransaction();
			if (jtaTx != null) {
				int jtaStatus = jtaTx.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					logger.debug("Registering JTA transaction synchronization for new Hibernate Session");
					SessionHolder holderToUse = sessionHolder;
					// Register JTA Transaction with existing SessionHolder.
					// Create a new SessionHolder if none existed before.
					if (holderToUse == null) {
						holderToUse = new SessionHolder(jtaTx, session);
					}
					else {
						holderToUse.addSession(jtaTx, session);
					}
					jtaTx.registerSynchronization(
							new SpringJtaSynchronizationAdapter(
									new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true),
									jtaTm));
					holderToUse.setSynchronizedWithTransaction(true);
					if (holderToUse != sessionHolder) {
						TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse);
					}
				}
			}
		}
		catch (Throwable ex) {
			throw new DataAccessResourceFailureException(
					"Could not register synchronization with JTA TransactionManager", ex);
		}
	}
}
 
Example #15
Source File: DefaultJdoDialect.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Return the JDBC exception translator for this dialect, if any.
 */
public SQLExceptionTranslator getJdbcExceptionTranslator() {
	return this.jdbcExceptionTranslator;
}
 
Example #16
Source File: HibernateTransactionManager.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the JDBC exception translator for this transaction manager, if any.
 */
public SQLExceptionTranslator getJdbcExceptionTranslator() {
	return this.jdbcExceptionTranslator;
}
 
Example #17
Source File: HibernateTransactionManager.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Return the JDBC exception translator for this transaction manager, if any.
 */
public SQLExceptionTranslator getJdbcExceptionTranslator() {
	return this.jdbcExceptionTranslator;
}
 
Example #18
Source File: SessionFactoryUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Register a JTA synchronization for the given Session, if any.
 * @param sessionHolder the existing thread-bound SessionHolder, if any
 * @param session the Session to register
 * @param sessionFactory the SessionFactory that the Session was created with
 * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the
 * Session on transaction synchronization (may be {@code null})
 */
private static void registerJtaSynchronization(Session session, SessionFactory sessionFactory,
		SQLExceptionTranslator jdbcExceptionTranslator, SessionHolder sessionHolder) {

	// JTA synchronization is only possible with a javax.transaction.TransactionManager.
	// We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified
	// in Hibernate configuration, it will contain a TransactionManager reference.
	TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, session);
	if (jtaTm != null) {
		try {
			Transaction jtaTx = jtaTm.getTransaction();
			if (jtaTx != null) {
				int jtaStatus = jtaTx.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					logger.debug("Registering JTA transaction synchronization for new Hibernate Session");
					SessionHolder holderToUse = sessionHolder;
					// Register JTA Transaction with existing SessionHolder.
					// Create a new SessionHolder if none existed before.
					if (holderToUse == null) {
						holderToUse = new SessionHolder(jtaTx, session);
					}
					else {
						holderToUse.addSession(jtaTx, session);
					}
					jtaTx.registerSynchronization(
							new SpringJtaSynchronizationAdapter(
									new SpringSessionSynchronization(holderToUse, sessionFactory, jdbcExceptionTranslator, true),
									jtaTm));
					holderToUse.setSynchronizedWithTransaction(true);
					if (holderToUse != sessionHolder) {
						TransactionSynchronizationManager.bindResource(sessionFactory, holderToUse);
					}
				}
			}
		}
		catch (Throwable ex) {
			throw new DataAccessResourceFailureException(
					"Could not register synchronization with JTA TransactionManager", ex);
		}
	}
}
 
Example #19
Source File: SessionFactoryUtils.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Retrieve a Session from the given SessionHolder, potentially from a
 * JTA transaction synchronization.
 * @param sessionHolder the SessionHolder to check
 * @param sessionFactory the SessionFactory to get the JTA TransactionManager from
 * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the
 * Session on transaction synchronization (may be {@code null})
 * @return the associated Session, if any
 * @throws DataAccessResourceFailureException if the Session couldn't be created
 */
private static Session getJtaSynchronizedSession(
		SessionHolder sessionHolder, SessionFactory sessionFactory,
		SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException {

	// JTA synchronization is only possible with a javax.transaction.TransactionManager.
	// We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified
	// in Hibernate configuration, it will contain a TransactionManager reference.
	TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession());
	if (jtaTm != null) {
		// Check whether JTA transaction management is active ->
		// fetch pre-bound Session for the current JTA transaction, if any.
		// (just necessary for JTA transaction suspension, with an individual
		// Hibernate Session per currently active/suspended transaction)
		try {
			// Look for transaction-specific Session.
			Transaction jtaTx = jtaTm.getTransaction();
			if (jtaTx != null) {
				int jtaStatus = jtaTx.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					Session session = sessionHolder.getValidatedSession(jtaTx);
					if (session == null && !sessionHolder.isSynchronizedWithTransaction()) {
						// No transaction-specific Session found: If not already marked as
						// synchronized with transaction, register the default thread-bound
						// Session as JTA-transactional. If there is no default Session,
						// we're a new inner JTA transaction with an outer one being suspended:
						// In that case, we'll return null to trigger opening of a new Session.
						session = sessionHolder.getValidatedSession();
						if (session != null) {
							logger.debug("Registering JTA transaction synchronization for existing Hibernate Session");
							sessionHolder.addSession(jtaTx, session);
							jtaTx.registerSynchronization(
									new SpringJtaSynchronizationAdapter(
											new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false),
											jtaTm));
							sessionHolder.setSynchronizedWithTransaction(true);
							// Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
							// with FlushMode.NEVER, which needs to allow flushing within the transaction.
							FlushMode flushMode = session.getFlushMode();
							if (flushMode.lessThan(FlushMode.COMMIT)) {
								session.setFlushMode(FlushMode.AUTO);
								sessionHolder.setPreviousFlushMode(flushMode);
							}
						}
					}
					return session;
				}
			}
			// No transaction active -> simply return default thread-bound Session, if any
			// (possibly from OpenSessionInViewFilter/Interceptor).
			return sessionHolder.getValidatedSession();
		}
		catch (Throwable ex) {
			throw new DataAccessResourceFailureException("Could not check JTA transaction", ex);
		}
	}
	else {
		// No JTA TransactionManager -> simply return default thread-bound Session, if any
		// (possibly from OpenSessionInViewFilter/Interceptor).
		return sessionHolder.getValidatedSession();
	}
}
 
Example #20
Source File: JdbcDaoSupport.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Return the SQLExceptionTranslator of this DAO's JdbcTemplate,
 * for translating SQLExceptions in custom JDBC access code.
 * @see org.springframework.jdbc.core.JdbcTemplate#getExceptionTranslator()
 */
protected final SQLExceptionTranslator getExceptionTranslator() {
	JdbcTemplate jdbcTemplate = getJdbcTemplate();
	Assert.state(jdbcTemplate != null, "No JdbcTemplate set");
	return jdbcTemplate.getExceptionTranslator();
}
 
Example #21
Source File: SessionFactoryUtils.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Retrieve a Session from the given SessionHolder, potentially from a
 * JTA transaction synchronization.
 * @param sessionHolder the SessionHolder to check
 * @param sessionFactory the SessionFactory to get the JTA TransactionManager from
 * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the
 * Session on transaction synchronization (may be {@code null})
 * @return the associated Session, if any
 * @throws DataAccessResourceFailureException if the Session couldn't be created
 */
private static Session getJtaSynchronizedSession(
		SessionHolder sessionHolder, SessionFactory sessionFactory,
		SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException {

	// JTA synchronization is only possible with a javax.transaction.TransactionManager.
	// We'll check the Hibernate SessionFactory: If a TransactionManagerLookup is specified
	// in Hibernate configuration, it will contain a TransactionManager reference.
	TransactionManager jtaTm = getJtaTransactionManager(sessionFactory, sessionHolder.getAnySession());
	if (jtaTm != null) {
		// Check whether JTA transaction management is active ->
		// fetch pre-bound Session for the current JTA transaction, if any.
		// (just necessary for JTA transaction suspension, with an individual
		// Hibernate Session per currently active/suspended transaction)
		try {
			// Look for transaction-specific Session.
			Transaction jtaTx = jtaTm.getTransaction();
			if (jtaTx != null) {
				int jtaStatus = jtaTx.getStatus();
				if (jtaStatus == Status.STATUS_ACTIVE || jtaStatus == Status.STATUS_MARKED_ROLLBACK) {
					Session session = sessionHolder.getValidatedSession(jtaTx);
					if (session == null && !sessionHolder.isSynchronizedWithTransaction()) {
						// No transaction-specific Session found: If not already marked as
						// synchronized with transaction, register the default thread-bound
						// Session as JTA-transactional. If there is no default Session,
						// we're a new inner JTA transaction with an outer one being suspended:
						// In that case, we'll return null to trigger opening of a new Session.
						session = sessionHolder.getValidatedSession();
						if (session != null) {
							logger.debug("Registering JTA transaction synchronization for existing Hibernate Session");
							sessionHolder.addSession(jtaTx, session);
							jtaTx.registerSynchronization(
									new SpringJtaSynchronizationAdapter(
											new SpringSessionSynchronization(sessionHolder, sessionFactory, jdbcExceptionTranslator, false),
											jtaTm));
							sessionHolder.setSynchronizedWithTransaction(true);
							// Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
							// with FlushMode.NEVER, which needs to allow flushing within the transaction.
							FlushMode flushMode = session.getFlushMode();
							if (flushMode.lessThan(FlushMode.COMMIT)) {
								session.setFlushMode(FlushMode.AUTO);
								sessionHolder.setPreviousFlushMode(flushMode);
							}
						}
					}
					return session;
				}
			}
			// No transaction active -> simply return default thread-bound Session, if any
			// (possibly from OpenSessionInViewFilter/Interceptor).
			return sessionHolder.getValidatedSession();
		}
		catch (Throwable ex) {
			throw new DataAccessResourceFailureException("Could not check JTA transaction", ex);
		}
	}
	else {
		// No JTA TransactionManager -> simply return default thread-bound Session, if any
		// (possibly from OpenSessionInViewFilter/Interceptor).
		return sessionHolder.getValidatedSession();
	}
}
 
Example #22
Source File: HibernateAccessor.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Return the JDBC exception translator for this instance, if any.
 */
public SQLExceptionTranslator getJdbcExceptionTranslator() {
	return this.jdbcExceptionTranslator;
}
 
Example #23
Source File: HibernateAccessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the JDBC exception translator for this instance, if any.
 */
public SQLExceptionTranslator getJdbcExceptionTranslator() {
	return this.jdbcExceptionTranslator;
}
 
Example #24
Source File: JdbcDaoSupport.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return the SQLExceptionTranslator of this DAO's JdbcTemplate,
 * for translating SQLExceptions in custom JDBC access code.
 * @see org.springframework.jdbc.core.JdbcTemplate#getExceptionTranslator()
 */
protected final SQLExceptionTranslator getExceptionTranslator() {
	JdbcTemplate jdbcTemplate = getJdbcTemplate();
	Assert.state(jdbcTemplate != null, "No JdbcTemplate set");
	return jdbcTemplate.getExceptionTranslator();
}
 
Example #25
Source File: DefaultJdoDialect.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the JDBC exception translator for this dialect, if any.
 */
public SQLExceptionTranslator getJdbcExceptionTranslator() {
	return this.jdbcExceptionTranslator;
}
 
Example #26
Source File: JooqExceptionTranslator.java    From micronaut-sql with Apache License 2.0 4 votes vote down vote up
private DataAccessException translate(ExecuteContext context,
        SQLExceptionTranslator translator, SQLException exception) {
    return translator.translate("jOOQ", context.sql(), exception);
}
 
Example #27
Source File: BatchJdbcTemplate.java    From buffer-slayer with Apache License 2.0 4 votes vote down vote up
public SQLExceptionTranslator getExceptionTranslator() {
  return delegate.getExceptionTranslator();
}
 
Example #28
Source File: BatchJdbcTemplate.java    From buffer-slayer with Apache License 2.0 4 votes vote down vote up
public void setExceptionTranslator(
    SQLExceptionTranslator exceptionTranslator) {
  delegate.setExceptionTranslator(exceptionTranslator);
}
 
Example #29
Source File: SessionFactoryUtils.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Get a Hibernate Session for the given SessionFactory. Is aware of and will
 * return any existing corresponding Session bound to the current thread, for
 * example when using {@link HibernateTransactionManager}. Will always create
 * a new Session otherwise.
 * <p>Supports setting a Session-level Hibernate entity interceptor that allows
 * to inspect and change property values before writing to and reading from the
 * database. Such an interceptor can also be set at the SessionFactory level
 * (i.e. on LocalSessionFactoryBean), on HibernateTransactionManager, etc.
 * @param sessionFactory Hibernate SessionFactory to create the session with
 * @param entityInterceptor Hibernate entity interceptor, or {@code null} if none
 * @param jdbcExceptionTranslator SQLExcepionTranslator to use for flushing the
 * Session on transaction synchronization (may be {@code null}; only used
 * when actually registering a transaction synchronization)
 * @return the Hibernate Session
 * @throws DataAccessResourceFailureException if the Session couldn't be created
 * @see LocalSessionFactoryBean#setEntityInterceptor
 * @see HibernateTemplate#setEntityInterceptor
 */
public static Session getSession(
		SessionFactory sessionFactory, Interceptor entityInterceptor,
		SQLExceptionTranslator jdbcExceptionTranslator) throws DataAccessResourceFailureException {

	try {
		return doGetSession(sessionFactory, entityInterceptor, jdbcExceptionTranslator, true);
	}
	catch (HibernateException ex) {
		throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
	}
}
 
Example #30
Source File: HibernateAccessor.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Convert the given SQLException to an appropriate exception from the
 * {@code org.springframework.dao} hierarchy. Can be overridden in subclasses.
 * <p>Note that a direct SQLException can just occur when callback code
 * performs direct JDBC access via {@code Session.connection()}.
 * @param ex the SQLException
 * @return the corresponding DataAccessException instance
 * @see #setJdbcExceptionTranslator
 */
protected DataAccessException convertJdbcAccessException(SQLException ex) {
	SQLExceptionTranslator translator = getJdbcExceptionTranslator();
	if (translator == null) {
		translator = getDefaultJdbcExceptionTranslator();
	}
	return translator.translate("Hibernate-related JDBC operation", null, ex);
}