Example usage for java.sql SQLException initCause

List of usage examples for java.sql SQLException initCause

Introduction

In this page you can find the example usage for java.sql SQLException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:org.apache.openjpa.jdbc.kernel.TableJDBCSeq.java

/**
 * Updates the max available sequence value.
 *//*from w w  w.ja  v  a 2 s . c  o m*/
private void allocateSequence(JDBCStore store, ClassMapping mapping, Status stat, int alloc,
        boolean updateStatSeq) throws SQLException {
    Runnable runnable = new AllocateSequenceRunnable(store, mapping, stat, alloc, updateStatSeq);
    try {
        if (suspendInJTA()) {
            // NotSupportedException is wrapped in a StoreException by
            // the caller.
            try {
                _conf.getManagedRuntimeInstance().doNonTransactionalWork(runnable);
            } catch (NotSupportedException nse) {
                SQLException sqlEx = new SQLException(nse.getLocalizedMessage());
                sqlEx.initCause(nse);
                throw sqlEx;
            }
        } else {
            runnable.run();
        }
    } catch (RuntimeException re) {
        Throwable e = re.getCause();
        if (e instanceof SQLException)
            throw (SQLException) e;
        else
            throw re;
    }
}

From source file:org.apereo.portal.portlet.dao.jpa.SQLNextExceptionLoggerAspect.java

public void logBatchUpdateExceptions(Throwable t) {
    while (!(t instanceof SQLException)) {
        t = t.getCause();/*from ww w.  j  a v a  2s.  c  o  m*/
        if (t == null) {
            return;
        }
    }

    SQLException sqle = (SQLException) t;

    //If the SQLException is the root chain the results of getNextException as initCauses
    if (sqle.getCause() == null) {
        SQLException nextException;
        while ((nextException = sqle.getNextException()) != null) {
            sqle.initCause(nextException);
            sqle = nextException;
        }
    }
    //The SQLException already has a cause so log the results of all getNextException calls
    else {
        while ((sqle = sqle.getNextException()) != null) {
            this.logger.error("Logging getNextException for root SQLException: " + t, sqle);
        }
    }
}

From source file:org.eclipse.ecr.core.storage.sql.extensions.H2Fulltext.java

private static SQLException convertException(Exception e) {
    SQLException e2 = new SQLException("Error while indexing document");
    e2.initCause(e);
    return e2;/*from w w  w.  j  a v  a 2 s.c  o  m*/
}

From source file:org.intermine.sql.Database.java

/**
 * Executes an SQL statement on the database in a separate thread. A certain number of worker
 * threads controlled by the "parallel" property will operate simultaneously.
 *
 * @param sql an SQL string./*from   w  w  w  . j  a  v a 2  s .  c om*/
 * @throws SQLException if an error has been reported by a previous operation - however, this
 * does not cancel the current operation.
 */
public void executeSqlInParallel(String sql) throws SQLException {
    SqlJob job = new SqlJob(sql);
    synchronized (this) {
        pending.add(job);
    }
    boolean notDone = true;
    while (notDone) {
        try {
            queue.put(job);
            notDone = false;
        } catch (InterruptedException e) {
            // Not done
        }
    }
    synchronized (this) {
        if (workers.size() < parallel) {
            Worker worker = new Worker(threadNo);
            Thread thread = new Thread(worker);
            thread.setDaemon(true);
            thread.setName("Database background thread " + threadNo);
            threadNo++;
            workers.add(worker);
            thread.start();
        }
        if (reportedException != null) {
            SQLException re = new SQLException("Error while executing SQL");
            re.initCause(reportedException);
            reportedException = null;
            throw re;
        }
    }
}

From source file:org.intermine.sql.Database.java

/**
 * Blocks until all the current pending jobs are finished. This will not block new jobs from
 * arriving, and those new jobs do not need to be finished for this method to return.
 *
 * @throws SQLException if an error has been reported by a previous operation
 *//*www .jav  a  2 s.  c  om*/
public void waitForCurrentJobs() throws SQLException {
    Waiter waiter;
    synchronized (this) {
        waiter = new Waiter(pending);
        waiters.add(waiter);
    }
    waiter.waitUntilFinished();
    synchronized (this) {
        waiters.remove(waiter);
        if (reportedException != null) {
            SQLException re = new SQLException("Error while executing SQL");
            re.initCause(reportedException);
            reportedException = null;
            throw re;
        }
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.db.H2Fulltext.java

private static SQLException convertException(String message, Exception e) {
    SQLException e2 = new SQLException(message);
    e2.initCause(e);
    return e2;//from  w  w w  . ja  v  a2s .co  m
}

From source file:org.opennms.core.test.db.TemporaryDatabase.java

private void destroyTestDatabase() throws Exception {
    if (m_useExisting) {
        return;//from  w  w  w .ja v a 2s .  com
    }

    if (m_destroyed) {
        System.err.println("Database '" + getTestDatabase() + "' already destroyed");
        // database already destroyed
        return;
    }

    /*
    * Sleep before destroying the test database because PostgreSQL doesn't
    * seem to notice immediately clients have disconnected. Yeah, it's a
    * hack.
    */
    Thread.sleep(100);

    Connection adminConnection = getAdminDataSource().getConnection();

    try {
        for (int dropAttempt = 0; dropAttempt < MAX_DATABASE_DROP_ATTEMPTS; dropAttempt++) {
            Statement st = null;

            try {
                st = adminConnection.createStatement();
                st.execute("DROP DATABASE " + getTestDatabase());
                break;
            } catch (SQLException e) {
                if ((dropAttempt + 1) >= MAX_DATABASE_DROP_ATTEMPTS) {
                    final String message = "Failed to drop test database on last attempt " + (dropAttempt + 1)
                            + ": " + e;
                    System.err.println(new Date().toString() + ": " + message);
                    dumpThreads();

                    SQLException newException = new SQLException(message);
                    newException.initCause(e);
                    throw newException;
                } else {
                    System.err.println(new Date().toString() + ": Failed to drop test database on attempt "
                            + (dropAttempt + 1) + ": " + e);
                    Thread.sleep(1000);
                }
            } finally {
                if (st != null) {
                    st.close();
                    st = null;
                }
            }
        }
    } finally {
        /*
         * Since we are already going to be throwing an exception at this
         * point, print any further errors to stdout so we don't mask the
         * first failure.
         */
        try {
            adminConnection.close();
        } catch (SQLException e) {
            System.err.println("Error closing administrative database " + "connection after attempting to drop "
                    + "test database");
            e.printStackTrace();
        }

        /*
         * Sleep after disconnecting from template1, otherwise creating a
         * new test database in future tests may fail. Man, I hate this.
         */
        Thread.sleep(100);
    }

    m_destroyed = true;
}

From source file:org.sonar.jpa.session.DriverDatabaseConnector.java

public Connection getConnection() throws SQLException {
    try {//from  w  ww .  jav  a  2  s  . c o  m
        /*
          The sonar batch downloads the JDBC driver in a separated classloader.
          This is a well-know problem of java.sql.DriverManager. The workaround
          is to use a proxy.
          See http://stackoverflow.com/questions/288828/how-to-use-a-jdbc-driver-from-an-arbitrary-location
         */
        Driver driver = (Driver) classloader.loadClass(getDriver()).newInstance();
        DriverManager.registerDriver(new DriverProxy(driver));

    } catch (Exception e) {
        SQLException ex = new SQLException("SQL driver not found " + getDriver());
        ex.initCause(e);
        throw ex;
    }
    return DriverManager.getConnection(getUrl(), getUsername(), getPassword());
}