Search in sources :

Example 66 with DataAccessException

use of org.springframework.dao.DataAccessException in project spring-framework by spring-projects.

the class NamedParameterJdbcTemplateTests method testQueryWithResultSetExtractorNoParameters.

@Test
public void testQueryWithResultSetExtractorNoParameters() throws SQLException {
    given(resultSet.next()).willReturn(true);
    given(resultSet.getInt("id")).willReturn(1);
    given(resultSet.getString("forename")).willReturn("rod");
    Customer cust = namedParameterTemplate.query(SELECT_NO_PARAMETERS, new ResultSetExtractor<Customer>() {

        @Override
        public Customer extractData(ResultSet rs) throws SQLException, DataAccessException {
            rs.next();
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }
    });
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
    verify(connection).prepareStatement(SELECT_NO_PARAMETERS);
    verify(preparedStatement).close();
    verify(connection).close();
}
Also used : Customer(org.springframework.jdbc.Customer) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 67 with DataAccessException

use of org.springframework.dao.DataAccessException in project spring-framework by spring-projects.

the class SQLExceptionCustomTranslatorTests method badSqlGrammarException.

@Test
public void badSqlGrammarException() {
    SQLException badSqlGrammarExceptionEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 1);
    DataAccessException dae = sext.translate("task", "SQL", badSqlGrammarExceptionEx);
    assertEquals(badSqlGrammarExceptionEx, dae.getCause());
    assertThat(dae, instanceOf(BadSqlGrammarException.class));
}
Also used : BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) SQLException(java.sql.SQLException) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 68 with DataAccessException

use of org.springframework.dao.DataAccessException in project spring-security-oauth by spring-projects.

the class JdbcApprovalStore method purgeExpiredApprovals.

public boolean purgeExpiredApprovals() {
    logger.debug("Purging expired approvals from database");
    try {
        int deleted = jdbcTemplate.update(deleteApprovalStatment + " where expiresAt <= ?", new PreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setTimestamp(1, new Timestamp(new Date().getTime()));
            }
        });
        logger.debug(deleted + " expired approvals deleted");
    } catch (DataAccessException ex) {
        logger.error("Error purging expired approvals", ex);
        return false;
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Date(java.util.Date) DataAccessException(org.springframework.dao.DataAccessException)

Example 69 with DataAccessException

use of org.springframework.dao.DataAccessException in project spring-boot by spring-projects.

the class AbstractDataSourcePoolMetadataTests method getPoolSizeTwoConnections.

@Test
public void getPoolSizeTwoConnections() {
    final JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
    jdbcTemplate.execute(new ConnectionCallback<Void>() {

        @Override
        public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
            jdbcTemplate.execute(new ConnectionCallback<Void>() {

                @Override
                public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
                    assertThat(getDataSourceMetadata().getActive()).isEqualTo(2);
                    assertThat(getDataSourceMetadata().getUsage()).isEqualTo(1.0f);
                    return null;
                }
            });
            return null;
        }
    });
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ConnectionCallback(org.springframework.jdbc.core.ConnectionCallback) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Example 70 with DataAccessException

use of org.springframework.dao.DataAccessException in project spring-boot by spring-projects.

the class AbstractDataSourcePoolMetadataTests method getPoolSizeNoConnection.

@Test
public void getPoolSizeNoConnection() {
    // Make sure the pool is initialized
    JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSourceMetadata().getDataSource());
    jdbcTemplate.execute(new ConnectionCallback<Void>() {

        @Override
        public Void doInConnection(Connection connection) throws SQLException, DataAccessException {
            return null;
        }
    });
    assertThat(getDataSourceMetadata().getActive()).isEqualTo(Integer.valueOf(0));
    assertThat(getDataSourceMetadata().getUsage()).isEqualTo(Float.valueOf(0));
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.Test)

Aggregations

DataAccessException (org.springframework.dao.DataAccessException)89 SQLException (java.sql.SQLException)40 Test (org.junit.Test)26 Connection (java.sql.Connection)17 ResultSet (java.sql.ResultSet)16 PreparedStatement (java.sql.PreparedStatement)14 MongoException (com.mongodb.MongoException)13 Document (org.bson.Document)8 TransactionStatus (org.springframework.transaction.TransactionStatus)7 HashMap (java.util.HashMap)6 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)5 DeadlockLoserDataAccessException (org.springframework.dao.DeadlockLoserDataAccessException)5 IOException (java.io.IOException)4 ConnectionCallback (org.springframework.jdbc.core.ConnectionCallback)4 SpringSqlParams (com.opengamma.elsql.SpringSqlParams)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 Statement (java.sql.Statement)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 IJoinQueryString (org.apereo.portal.jdbc.IJoinQueryString)3