Java Code Examples for org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator

The following examples show how to use org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: JdbcTemplateTests.java    License: 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 Project: java-technology-stack   Source File: JdbcTemplateTests.java    License: 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 Project: spring4-understanding   Source File: JdbcTemplateTests.java    License: 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 Project: gorm-hibernate5   Source File: GrailsHibernateTemplate.java    License: 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 Project: effectivejava   Source File: JdbcTemplateTests.java    License: 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
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 Project: micronaut-sql   Source File: JooqExceptionTranslator.java    License: 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
/**
 * 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
/**
 * 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 Project: tutorials   Source File: ExceptionTranslator.java    License: 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
/**
 * 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 Project: lams   Source File: SessionFactoryUtils.java    License: 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
/**
 * 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 Project: spring4-understanding   Source File: SessionFactoryUtils.java    License: 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();
}