Search in sources :

Example 31 with SQLFeatureNotSupportedException

use of java.sql.SQLFeatureNotSupportedException in project tomee by apache.

the class BasicManagedDataSource method getParentLogger.

public Logger getParentLogger() throws SQLFeatureNotSupportedException {
    final ReentrantLock l = lock;
    l.lock();
    try {
        if (null == this.logger) {
            this.logger = (Logger) DataSource.class.getDeclaredMethod("getParentLogger").invoke(createDataSource());
        }
        return this.logger;
    } catch (final Throwable e) {
        throw new SQLFeatureNotSupportedException();
    } finally {
        l.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) ManagedDataSource(org.apache.commons.dbcp2.managed.ManagedDataSource) DataSource(javax.sql.DataSource) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource)

Example 32 with SQLFeatureNotSupportedException

use of java.sql.SQLFeatureNotSupportedException in project elasticsearch-jdbc by jprante.

the class StandardSource method getConnectionForReading.

/**
     * Get JDBC connection for reading
     *
     * @return the connection
     * @throws SQLException when SQL execution gives an error
     */
@Override
public synchronized Connection getConnectionForReading() throws SQLException {
    boolean invalid = readConnection == null || readConnection.isClosed();
    try {
        invalid = invalid || !readConnection.isValid(5);
    } catch (AbstractMethodError e) {
        // old/buggy JDBC driver
        logger.debug(e.getMessage());
    } catch (SQLFeatureNotSupportedException e) {
        // postgresql does not support isValid()
        logger.debug(e.getMessage());
    }
    if (invalid) {
        int retries = getRetries();
        while (retries > 0) {
            retries--;
            try {
                if (user != null) {
                    Properties properties = new Properties();
                    properties.put("user", user);
                    if (password != null) {
                        properties.put("password", password);
                    }
                    if (getConnectionProperties() != null) {
                        properties.putAll(getConnectionProperties());
                    }
                    readConnection = DriverManager.getConnection(url, properties);
                } else {
                    readConnection = DriverManager.getConnection(url);
                }
                DatabaseMetaData metaData = readConnection.getMetaData();
                if (shouldPrepareDatabaseMetadata()) {
                    prepare(metaData);
                }
                if (metaData.getTimeDateFunctions().contains("TIMESTAMPDIFF")) {
                    setTimestampDiffSupported(true);
                }
                // "readonly" is required by MySQL for large result streaming
                readConnection.setReadOnly(true);
                // Postgresql cursor mode condition:
                // fetchsize > 0, no scrollable result set, no auto commit, no holdable cursors over commit
                // https://github.com/pgjdbc/pgjdbc/blob/master/org/postgresql/jdbc2/AbstractJdbc2Statement.java#L514
                //readConnection.setHoldability(ResultSet.HOLD_CURSORS_OVER_COMMIT);
                // many drivers don't like autocommit=true
                readConnection.setAutoCommit(getAutoCommit());
                return readConnection;
            } catch (SQLException e) {
                logger.error("while opening read connection: " + url + " " + e.getMessage(), e);
                try {
                    logger.debug("delaying for {} seconds...", getMaxRetryWait().seconds());
                    Thread.sleep(getMaxRetryWait().millis());
                } catch (InterruptedException ex) {
                // do nothing
                }
            }
        }
    }
    return readConnection;
}
Also used : SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) Properties(java.util.Properties) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 33 with SQLFeatureNotSupportedException

use of java.sql.SQLFeatureNotSupportedException in project elasticsearch-jdbc by jprante.

the class StandardSource method execute.

/**
     * Execute SQL query command without parameter binding.
     *
     * @param command the SQL command
     * @throws SQLException when SQL execution gives an error
     * @throws IOException  when input/output error occurs
     */
private void execute(SQLCommand command) throws Exception {
    Statement statement = null;
    ResultSet results = null;
    try {
        if (command.isQuery()) {
            // use read connection
            // we must not use prepareStatement for Postgresql!
            // Postgresql requires direct use of executeQuery(sql) for cursor with fetchsize set.
            Connection connection = getConnectionForReading();
            if (connection != null) {
                logger.debug("{} using read connection {} for executing query", this, connection);
                statement = connection.createStatement();
                try {
                    statement.setQueryTimeout(getQueryTimeout());
                } catch (SQLFeatureNotSupportedException e) {
                    // Postgresql does not support setQueryTimeout()
                    logger.warn("driver does not support setQueryTimeout(), skipped");
                }
                results = executeQuery(statement, command.getSQL());
                if (shouldPrepareResultSetMetadata()) {
                    prepare(results.getMetaData());
                }
                SinkKeyValueStreamListener<Object, Object> listener = new SinkKeyValueStreamListener<Object, Object>().output(context.getSink()).shouldIgnoreNull(shouldIgnoreNull()).shouldDetectGeo(shouldDetectGeo()).shouldDetectJson(shouldDetectJson());
                merge(command, results, listener);
            }
        } else {
            // use write connection
            Connection connection = getConnectionForWriting();
            if (connection != null) {
                logger.debug("{} using write connection {} for executing insert/update", this, connection);
                statement = connection.createStatement();
                executeUpdate(statement, command.getSQL());
            }
        }
    } finally {
        close(results);
        close(statement);
    }
}
Also used : SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) SinkKeyValueStreamListener(org.xbib.elasticsearch.common.util.SinkKeyValueStreamListener)

Example 34 with SQLFeatureNotSupportedException

use of java.sql.SQLFeatureNotSupportedException in project robovm by robovm.

the class OldStatementTest method testExecuteUpdate_String_intArray.

// TODO executeUpdate(String sql, int[] columnIndexes) is not supported
public void testExecuteUpdate_String_intArray() throws SQLException {
    Statement st = null;
    try {
        String[] queries1 = { "update zoo set name='Masha', family='cat' where id=2;", "drop table if exists hutch", "create table hutch (id integer not null, animal_id integer, address char(20), primary key (id));", "insert into hutch (id, animal_id, address) values (1, 2, 'Birds-house, 1');", "insert into hutch (id, animal_id, address) values (2, 1, 'Horse-house, 5');", "create view address as select address from hutch where animal_id=2", "drop view address;", "drop table hutch;" };
        Vector<int[]> array = new Vector<int[]>();
        array.addElement(null);
        array.addElement(new int[] { 1, 2, 3 });
        array.addElement(new int[] { 1, 2, 10, 100 });
        array.addElement(new int[] {});
        array.addElement(new int[] { 100, 200 });
        array.addElement(new int[] { -1, 0 });
        array.addElement(new int[] { 0, 0, 0, 1, 2, 3 });
        array.addElement(new int[] { -100, -200 });
        st = conn.createStatement();
        for (int i = 0; i < queries1.length; i++) {
            st.executeUpdate(queries1[i], (int[]) array.elementAt(i));
            fail("Exception expected");
        }
    } catch (SQLFeatureNotSupportedException e) {
    // expected
    } finally {
        try {
            st.close();
        } catch (SQLException ee) {
        }
    }
}
Also used : SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Vector(java.util.Vector)

Example 35 with SQLFeatureNotSupportedException

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

the class MySqlUtils method createXAConnection.

public static XAConnection createXAConnection(Driver driver, Connection physicalConn) throws SQLException {
    final int major = driver.getMajorVersion();
    if (major == 5) {
        if (utilClass == null && !utilClassError) {
            try {
                utilClass = Class.forName("com.mysql.jdbc.Util");
                Method method = utilClass.getMethod("isJdbc4");
                utilClass_isJdbc4 = (Boolean) method.invoke(null);
                connectionClass = Class.forName("com.mysql.jdbc.Connection");
                getPinGlobalTxToPhysicalConnectionMethod = connectionClass.getMethod("getPinGlobalTxToPhysicalConnection");
                suspendableXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.SuspendableXAConnection");
                suspendableXAConnectionConstructor = suspendableXAConnectionClass.getConstructor(connectionClass);
                JDBC4SuspendableXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.JDBC4SuspendableXAConnection");
                JDBC4SuspendableXAConnectionConstructor = JDBC4SuspendableXAConnectionClass.getConstructor(connectionClass);
                MysqlXAConnectionClass = Class.forName("com.mysql.jdbc.jdbc2.optional.MysqlXAConnection");
                MysqlXAConnectionConstructor = MysqlXAConnectionClass.getConstructor(connectionClass, boolean.class);
            } catch (Exception ex) {
                ex.printStackTrace();
                utilClassError = true;
            }
        }
        try {
            boolean pinGlobTx = (Boolean) getPinGlobalTxToPhysicalConnectionMethod.invoke(physicalConn);
            if (pinGlobTx) {
                if (!utilClass_isJdbc4) {
                    return (XAConnection) suspendableXAConnectionConstructor.newInstance(physicalConn);
                }
                return (XAConnection) JDBC4SuspendableXAConnectionConstructor.newInstance(physicalConn);
            }
            return (XAConnection) MysqlXAConnectionConstructor.newInstance(physicalConn, Boolean.FALSE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    throw new SQLFeatureNotSupportedException();
}
Also used : SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) Method(java.lang.reflect.Method) SQLException(java.sql.SQLException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) InvocationTargetException(java.lang.reflect.InvocationTargetException) XAConnection(javax.sql.XAConnection)

Aggregations

SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)55 SQLException (java.sql.SQLException)20 Connection (java.sql.Connection)14 Test (org.testng.annotations.Test)14 BaseTest (util.BaseTest)14 PreparedStatement (java.sql.PreparedStatement)13 Statement (java.sql.Statement)11 ResultSet (java.sql.ResultSet)10 Test (org.junit.Test)5 Properties (java.util.Properties)4 CallableStatement (java.sql.CallableStatement)3 DruidPooledCallableStatement (com.alibaba.druid.pool.DruidPooledCallableStatement)2 DruidPooledResultSet (com.alibaba.druid.pool.DruidPooledResultSet)2 DruidPooledStatement (com.alibaba.druid.pool.DruidPooledStatement)2 Date (java.sql.Date)2 SQLDataException (java.sql.SQLDataException)2 Savepoint (java.sql.Savepoint)2 Vector (java.util.Vector)2 ColumnInfo (org.apache.jena.jdbc.results.metadata.columns.ColumnInfo)2 SparqlColumnInfo (org.apache.jena.jdbc.results.metadata.columns.SparqlColumnInfo)2