org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator Java Examples

The following examples show how to use org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator. 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: JdbcTemplateTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * If beanProperty is true, initialize via exception translator bean property;
 * if false, use afterPropertiesSet().
 */
private void doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty)
		throws SQLException {

	SQLException sqlException = new SQLException("foo", "07xxx");
	this.dataSource = mock(DataSource.class);
	given(this.dataSource.getConnection()).willThrow(sqlException);
	this.template = new JdbcTemplate();
	this.template.setDataSource(this.dataSource);
	this.template.setLazyInit(false);
	if (beanProperty) {
		// This will get a connection.
		this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(this.dataSource));
	}
	else {
		// This will cause creation of default SQL translator.
		this.template.afterPropertiesSet();
	}
	RowCountCallbackHandler rcch = new RowCountCallbackHandler();
	assertThatExceptionOfType(CannotGetJdbcConnectionException.class).isThrownBy(() ->
			this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch))
		.withCause(sqlException);
}
 
Example #2
Source File: JdbcTemplateTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * If beanProperty is true, initialize via exception translator bean property;
 * if false, use afterPropertiesSet().
 */
private void doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty)
		throws SQLException {

	SQLException sqlException = new SQLException("foo", "07xxx");
	this.dataSource = mock(DataSource.class);
	given(this.dataSource.getConnection()).willThrow(sqlException);
	this.template = new JdbcTemplate();
	this.template.setDataSource(this.dataSource);
	this.template.setLazyInit(false);
	if (beanProperty) {
		// This will get a connection.
		this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(this.dataSource));
	}
	else {
		// This will cause creation of default SQL translator.
		this.template.afterPropertiesSet();
	}
	RowCountCallbackHandler rcch = new RowCountCallbackHandler();
	this.thrown.expect(CannotGetJdbcConnectionException.class);
	this.thrown.expect(exceptionCause(sameInstance(sqlException)));
	this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
}
 
Example #3
Source File: JdbcTemplateTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * If beanProperty is true, initialize via exception translator bean property;
 * if false, use afterPropertiesSet().
 */
private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty)
		throws SQLException {
	SQLException sqlException = new SQLException("foo", "07xxx");
	this.dataSource = mock(DataSource.class);
	given(this.dataSource.getConnection()).willThrow(sqlException);
	this.template = new JdbcTemplate();
	this.template.setDataSource(this.dataSource);
	this.template.setLazyInit(false);
	if (beanProperty) {
		// This will get a connection.
		this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(this.dataSource));
	}
	else {
		// This will cause creation of default SQL translator.
		this.template.afterPropertiesSet();
	}
	RowCountCallbackHandler rcch = new RowCountCallbackHandler();
	this.thrown.expect(CannotGetJdbcConnectionException.class);
	this.thrown.expect(exceptionCause(sameInstance(sqlException)));
	this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
}
 
Example #4
Source File: GrailsHibernateTemplate.java    From gorm-hibernate5 with Apache License 2.0 6 votes vote down vote up
public GrailsHibernateTemplate(SessionFactory sessionFactory) {
    Assert.notNull(sessionFactory, "Property 'sessionFactory' is required");
    this.sessionFactory = sessionFactory;

    ConnectionProvider connectionProvider = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry().getService(ConnectionProvider.class);
    if(connectionProvider instanceof DatasourceConnectionProviderImpl) {
        this.dataSource = ((DatasourceConnectionProviderImpl) connectionProvider).getDataSource();
        if(dataSource instanceof TransactionAwareDataSourceProxy) {
            this.dataSource = ((TransactionAwareDataSourceProxy) dataSource).getTargetDataSource();
        }
        jdbcExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource);
    }
    else {
        // must be in unit test mode, setup default translator
        SQLErrorCodeSQLExceptionTranslator sqlErrorCodeSQLExceptionTranslator = new SQLErrorCodeSQLExceptionTranslator();
        sqlErrorCodeSQLExceptionTranslator.setDatabaseProductName("H2");
        jdbcExceptionTranslator = sqlErrorCodeSQLExceptionTranslator;
    }
}
 
Example #5
Source File: JdbcTemplateTests.java    From effectivejava with Apache License 2.0 6 votes vote down vote up
/**
 * If beanProperty is true, initialize via exception translator bean property;
 * if false, use afterPropertiesSet().
 */
private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty)
		throws SQLException {
	SQLException sqlException = new SQLException("foo", "07xxx");
	this.dataSource = mock(DataSource.class);
	given(this.dataSource.getConnection()).willThrow(sqlException);
	this.template = new JdbcTemplate();
	this.template.setDataSource(this.dataSource);
	this.template.setLazyInit(false);
	if (beanProperty) {
		// This will get a connection.
		this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(this.dataSource));
	}
	else {
		// This will cause creation of default SQL translator.
		this.template.afterPropertiesSet();
	}
	RowCountCallbackHandler rcch = new RowCountCallbackHandler();
	this.thrown.expect(CannotGetJdbcConnectionException.class);
	this.thrown.expect(exceptionCause(sameInstance(sqlException)));
	this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
}
 
Example #6
Source File: DefaultConcurrentRequestProcessor.java    From cobarclient with Apache License 2.0 6 votes vote down vote up
protected Object executeWith(Connection connection, SqlMapClientCallback action) {
    SqlMapSession session = getSqlMapClient().openSession();
    try {
        try {
            session.setUserConnection(connection);
        } catch (SQLException e) {
            throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", e);
        }
        try {
            return action.doInSqlMapClient(session);
        } catch (SQLException ex) {
            throw new SQLErrorCodeSQLExceptionTranslator().translate("SqlMapClient operation",
                    null, ex);
        }
    } finally {
        session.close();
    }
}
 
Example #7
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 #8
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 #9
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 #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: PersistenceManagerFactoryUtils.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create an appropriate SQLExceptionTranslator for the given PersistenceManagerFactory.
 * <p>If a DataSource is found, creates a SQLErrorCodeSQLExceptionTranslator for the
 * DataSource; else, falls back to a SQLStateSQLExceptionTranslator.
 * @param connectionFactory the connection factory of the PersistenceManagerFactory
 * (may be {@code null})
 * @return the SQLExceptionTranslator (never {@code null})
 * @see javax.jdo.PersistenceManagerFactory#getConnectionFactory()
 * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
 * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
 */
static SQLExceptionTranslator newJdbcExceptionTranslator(Object connectionFactory) {
	// Check for PersistenceManagerFactory's DataSource.
	if (connectionFactory instanceof DataSource) {
		return new SQLErrorCodeSQLExceptionTranslator((DataSource) connectionFactory);
	}
	else {
		return new SQLStateSQLExceptionTranslator();
	}
}
 
Example #12
Source File: SessionFactoryUtils.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create an appropriate SQLExceptionTranslator for the given SessionFactory.
 * If a DataSource is found, a SQLErrorCodeSQLExceptionTranslator for the DataSource
 * is created; else, a SQLStateSQLExceptionTranslator as fallback.
 * @param sessionFactory the SessionFactory to create the translator for
 * @return the SQLExceptionTranslator
 * @see #getDataSource
 * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
 * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
 */
public static SQLExceptionTranslator newJdbcExceptionTranslator(SessionFactory sessionFactory) {
	DataSource ds = getDataSource(sessionFactory);
	if (ds != null) {
		return new SQLErrorCodeSQLExceptionTranslator(ds);
	}
	return new SQLStateSQLExceptionTranslator();
}
 
Example #13
Source File: PersistenceManagerFactoryUtils.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Create an appropriate SQLExceptionTranslator for the given PersistenceManagerFactory.
 * <p>If a DataSource is found, creates a SQLErrorCodeSQLExceptionTranslator for the
 * DataSource; else, falls back to a SQLStateSQLExceptionTranslator.
 * @param connectionFactory the connection factory of the PersistenceManagerFactory
 * (may be {@code null})
 * @return the SQLExceptionTranslator (never {@code null})
 * @see javax.jdo.PersistenceManagerFactory#getConnectionFactory()
 * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
 * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
 */
static SQLExceptionTranslator newJdbcExceptionTranslator(Object connectionFactory) {
	// Check for PersistenceManagerFactory's DataSource.
	if (connectionFactory instanceof DataSource) {
		return new SQLErrorCodeSQLExceptionTranslator((DataSource) connectionFactory);
	}
	else {
		return new SQLStateSQLExceptionTranslator();
	}
}
 
Example #14
Source File: SessionFactoryUtils.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Create an appropriate SQLExceptionTranslator for the given SessionFactory.
 * If a DataSource is found, a SQLErrorCodeSQLExceptionTranslator for the DataSource
 * is created; else, a SQLStateSQLExceptionTranslator as fallback.
 * @param sessionFactory the SessionFactory to create the translator for
 * @return the SQLExceptionTranslator
 * @see #getDataSource
 * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
 * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
 */
public static SQLExceptionTranslator newJdbcExceptionTranslator(SessionFactory sessionFactory) {
	DataSource ds = getDataSource(sessionFactory);
	if (ds != null) {
		return new SQLErrorCodeSQLExceptionTranslator(ds);
	}
	return new SQLStateSQLExceptionTranslator();
}