Search in sources :

Example 66 with SQLWarning

use of java.sql.SQLWarning in project spring-framework by spring-projects.

the class JdbcTemplateTests method testFatalWarning.

/**
 * Mock objects allow us to produce warnings at will
 */
@Test
public void testFatalWarning() throws Exception {
    String sql = "SELECT forename from custmr";
    SQLWarning warnings = new SQLWarning("My warning");
    given(this.resultSet.next()).willReturn(false);
    given(this.preparedStatement.getWarnings()).willReturn(warnings);
    given(this.connection.createStatement()).willReturn(this.preparedStatement);
    JdbcTemplate t = new JdbcTemplate(this.dataSource);
    t.setIgnoreWarnings(false);
    ResultSetExtractor<Byte> extractor = rs -> rs.getByte(1);
    assertThatExceptionOfType(SQLWarningException.class).isThrownBy(() -> t.query(sql, extractor)).withCause(warnings);
    verify(this.resultSet).close();
    verify(this.preparedStatement).close();
    verify(this.connection).close();
}
Also used : SQLWarning(java.sql.SQLWarning) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) DataAccessException(org.springframework.dao.DataAccessException) Connection(java.sql.Connection) BatchUpdateException(java.sql.BatchUpdateException) LinkedCaseInsensitiveMap(org.springframework.util.LinkedCaseInsensitiveMap) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) DatabaseMetaData(java.sql.DatabaseMetaData) InvalidDataAccessApiUsageException(org.springframework.dao.InvalidDataAccessApiUsageException) ArrayList(java.util.ArrayList) SingleConnectionDataSource(org.springframework.jdbc.datasource.SingleConnectionDataSource) SQLException(java.sql.SQLException) SQLWarningException(org.springframework.jdbc.SQLWarningException) BDDMockito.given(org.mockito.BDDMockito.given) ResultSet(java.sql.ResultSet) Map(java.util.Map) DataSource(javax.sql.DataSource) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) SQLWarning(java.sql.SQLWarning) SQLErrorCodeSQLExceptionTranslator(org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator) CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) AbstractInterruptibleBatchPreparedStatementSetter(org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) Mockito.times(org.mockito.Mockito.times) ConnectionProxy(org.springframework.jdbc.datasource.ConnectionProxy) PreparedStatement(java.sql.PreparedStatement) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) Mockito.never(org.mockito.Mockito.never) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) SQLStateSQLExceptionTranslator(org.springframework.jdbc.support.SQLStateSQLExceptionTranslator) Collections(java.util.Collections) Mockito.reset(org.mockito.Mockito.reset) ResultSetMetaData(java.sql.ResultSetMetaData) Types(java.sql.Types) StringUtils(org.springframework.util.StringUtils) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 67 with SQLWarning

use of java.sql.SQLWarning in project druid by alibaba.

the class ResultSetProxyImpl method getWarnings.

@Override
public SQLWarning getWarnings() throws SQLException {
    FilterChainImpl chain = createChain();
    SQLWarning value = chain.resultSet_getWarnings(this);
    recycleFilterChain(chain);
    return value;
}
Also used : SQLWarning(java.sql.SQLWarning) FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl)

Example 68 with SQLWarning

use of java.sql.SQLWarning in project druid by alibaba.

the class ConnectionProxyImpl method getWarnings.

@Override
public SQLWarning getWarnings() throws SQLException {
    FilterChainImpl chain = createChain();
    SQLWarning value = chain.connection_getWarnings(this);
    recycleFilterChain(chain);
    return value;
}
Also used : SQLWarning(java.sql.SQLWarning) FilterChainImpl(com.alibaba.druid.filter.FilterChainImpl)

Example 69 with SQLWarning

use of java.sql.SQLWarning in project Payara by payara.

the class TransactionalAnnotationTest method testSpecRollbackOnDontRollbackOnSample.

public void testSpecRollbackOnDontRollbackOnSample() throws Exception {
    TransactionalInterceptorRequired transactionalInterceptorREQUIRED = new TransactionalInterceptorRequired();
    javax.transaction.TransactionManager transactionManager = new TransactionManager();
    transactionalInterceptorREQUIRED.setTestTransactionManager(transactionManager);
    javax.interceptor.InvocationContext ctx = new InvocationContext(BeanSpecExampleOfRollbackDontRollback.class.getMethod("throwSQLException"), null, BeanSpecExampleOfRollbackDontRollback.class) {

        @Override
        public Object getTarget() {
            return new BeanSpecExampleOfRollbackDontRollback();
        }

        @Override
        public Object proceed() throws Exception {
            throw new SQLException("test SQLException");
        }
    };
    transactionManager.begin();
    try {
        transactionalInterceptorREQUIRED.transactional(ctx);
    } catch (SQLException sqlex) {
    }
    try {
        transactionManager.commit();
        fail("should have thrown RollbackException due to mark for rollback");
    } catch (RollbackException rbe) {
    }
    // Now with a child of SQLException
    ctx = new InvocationContext(BeanSpecExampleOfRollbackDontRollback.class.getMethod("throwSQLException"), null, BeanSpecExampleOfRollbackDontRollback.class) {

        @Override
        public Object getTarget() {
            return new BeanSpecExampleOfRollbackDontRollback();
        }

        @Override
        public Object proceed() throws Exception {
            throw new SQLExceptionExtension();
        }
    };
    transactionManager.begin();
    try {
        transactionalInterceptorREQUIRED.transactional(ctx);
    } catch (SQLExceptionExtension sqlex) {
    }
    try {
        transactionManager.commit();
        fail("should have thrown RollbackException due to mark for rollback");
    } catch (RollbackException rbe) {
    }
    // now with a child of SQLException but one that is specified as dontRollback
    ctx = new InvocationContext(BeanSpecExampleOfRollbackDontRollback.class.getMethod("throwSQLWarning"), null, BeanSpecExampleOfRollbackDontRollback.class) {

        @Override
        public Object proceed() throws Exception {
            throw new SQLWarning("test SQLWarning");
        }

        @Override
        public Object getTarget() {
            return new BeanSpecExampleOfRollbackDontRollback();
        }
    };
    transactionManager.begin();
    try {
        transactionalInterceptorREQUIRED.transactional(ctx);
    } catch (SQLWarning sqlex) {
    }
    try {
        transactionManager.commit();
    } catch (Exception rbe) {
        fail("should not thrown Exception");
    }
    // now with a child of SQLWarning but one that is specified as rollback
    // ie testing this
    // @Transactional(
    // rollbackOn = {SQLException.class, SQLWarningExtension.class},
    // dontRollbackOn = {SQLWarning.class})
    // where dontRollbackOn=SQLWarning overrides rollbackOn=SQLException,
    // but rollbackOn=SQLWarningExtension overrides dontRollbackOn=SQLWarning
    // ie...
    // SQLException isAssignableFrom SQLWarning
    // SQLWarning isAssignableFrom SQLWarningExtensionExtension
    // SQLWarningExtensionExtension isAssignableFrom SQLWarningExtension
    ctx = new InvocationContext(BeanSpecExampleOfRollbackDontRollbackExtension.class.getMethod("throwSQLWarning"), null, BeanSpecExampleOfRollbackDontRollbackExtension.class) {

        @Override
        public Object proceed() throws Exception {
            throw new SQLWarningExtension();
        }

        @Override
        public Object getTarget() {
            return new BeanSpecExampleOfRollbackDontRollbackExtension();
        }
    };
    transactionManager.begin();
    try {
        transactionalInterceptorREQUIRED.transactional(ctx);
    } catch (SQLWarningExtension sqlex) {
    }
    try {
        transactionManager.commit();
        fail("should have thrown RollbackException due to mark for rollback");
    } catch (RollbackException rbe) {
    }
    // same as above test but with extension just to show continued inheritance...
    ctx = new InvocationContext(BeanSpecExampleOfRollbackDontRollbackExtension.class.getMethod("throwSQLWarning"), null, BeanSpecExampleOfRollbackDontRollbackExtension.class) {

        @Override
        public Object proceed() throws Exception {
            throw new SQLWarningExtensionExtension();
        }

        @Override
        public Object getTarget() {
            return new BeanSpecExampleOfRollbackDontRollbackExtension();
        }
    };
    transactionManager.begin();
    try {
        transactionalInterceptorREQUIRED.transactional(ctx);
    } catch (SQLWarningExtensionExtension sqlex) {
    }
    try {
        transactionManager.commit();
        fail("should have thrown RollbackException due to mark for rollback");
    } catch (RollbackException rbe) {
    }
}
Also used : SQLWarning(java.sql.SQLWarning) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) SQLException(java.sql.SQLException) TransactionRequiredException(javax.transaction.TransactionRequiredException) TransactionalException(javax.transaction.TransactionalException) RollbackException(javax.transaction.RollbackException) InvalidTransactionException(javax.transaction.InvalidTransactionException)

Example 70 with SQLWarning

use of java.sql.SQLWarning in project dbeaver by dbeaver.

the class SQLServerToolWithStatus method getExecuteStatistics.

@Override
public List<ToolStatus> getExecuteStatistics(OBJECT_TYPE object, SETTINGS settings, DBEPersistAction action, DBCSession session, DBCStatement dbStat) throws DBCException {
    List<ToolStatus> statusList = new ArrayList<>();
    try {
        int warnNum = 0;
        SQLWarning warning = ((JDBCStatement) dbStat).getWarnings();
        while (warning != null) {
            statusList.add(new ToolStatus(object, warning.getMessage()));
            warnNum++;
            warning = warning.getNextWarning();
        }
        if (warnNum == 0) {
            statusList.add(new ToolStatus(object, "Done"));
        }
    } catch (SQLException e) {
    // ignore
    }
    return statusList;
}
Also used : SQLWarning(java.sql.SQLWarning) JDBCStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList)

Aggregations

SQLWarning (java.sql.SQLWarning)153 SQLException (java.sql.SQLException)49 Statement (java.sql.Statement)37 PreparedStatement (java.sql.PreparedStatement)30 Connection (java.sql.Connection)26 ResultSet (java.sql.ResultSet)23 Test (org.testng.annotations.Test)19 Test (org.junit.Test)17 BaseTest (util.BaseTest)15 CallableStatement (java.sql.CallableStatement)13 ArrayList (java.util.ArrayList)9 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)8 ResultSetMetaData (java.sql.ResultSetMetaData)6 Expectations (org.jmock.Expectations)6 IOException (java.io.IOException)4 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)3 PrestoWarning (com.facebook.presto.spi.PrestoWarning)3 Properties (java.util.Properties)3 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)3 StatementContext (org.apache.derby.iapi.sql.conn.StatementContext)3