Search in sources :

Example 1 with DataAccessResourceFailureException

use of cn.taketoday.dao.DataAccessResourceFailureException in project today-infrastructure by TAKETODAY.

the class MySQLMaxValueIncrementer method getNextKey.

@Override
protected synchronized long getNextKey() throws DataAccessException {
    if (this.maxId == this.nextId) {
        /*
       * If useNewConnection is true, then we obtain a non-managed connection so our modifications
       * are handled in a separate transaction. If it is false, then we use the current transaction's
       * connection relying on the use of a non-transactional storage engine like MYISAM for the
       * incrementer table. We also use straight JDBC code because we need to make sure that the insert
       * and select are performed on the same connection (otherwise we can't be sure that last_insert_id()
       * returned the correct value).
       */
        Connection con = null;
        Statement stmt = null;
        boolean mustRestoreAutoCommit = false;
        try {
            if (this.useNewConnection) {
                con = getDataSource().getConnection();
                if (con.getAutoCommit()) {
                    mustRestoreAutoCommit = true;
                    con.setAutoCommit(false);
                }
            } else {
                con = DataSourceUtils.getConnection(getDataSource());
            }
            stmt = con.createStatement();
            if (!this.useNewConnection) {
                DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
            }
            // Increment the sequence column...
            String columnName = getColumnName();
            try {
                stmt.executeUpdate("update " + getIncrementerName() + " set " + columnName + " = last_insert_id(" + columnName + " + " + getCacheSize() + ") limit 1");
            } catch (SQLException ex) {
                throw new DataAccessResourceFailureException("Could not increment " + columnName + " for " + getIncrementerName() + " sequence table", ex);
            }
            // Retrieve the new max of the sequence column...
            ResultSet rs = stmt.executeQuery(VALUE_SQL);
            try {
                if (!rs.next()) {
                    throw new DataAccessResourceFailureException("last_insert_id() failed after executing an update");
                }
                this.maxId = rs.getLong(1);
            } finally {
                JdbcUtils.closeResultSet(rs);
            }
            this.nextId = this.maxId - getCacheSize() + 1;
        } catch (SQLException ex) {
            throw new DataAccessResourceFailureException("Could not obtain last_insert_id()", ex);
        } finally {
            JdbcUtils.closeStatement(stmt);
            if (con != null) {
                if (this.useNewConnection) {
                    try {
                        con.commit();
                        if (mustRestoreAutoCommit) {
                            con.setAutoCommit(true);
                        }
                    } catch (SQLException ignore) {
                        throw new DataAccessResourceFailureException("Unable to commit new sequence value changes for " + getIncrementerName());
                    }
                    JdbcUtils.closeConnection(con);
                } else {
                    DataSourceUtils.releaseConnection(con, getDataSource());
                }
            }
        }
    } else {
        this.nextId++;
    }
    return this.nextId;
}
Also used : SQLException(java.sql.SQLException) DataAccessResourceFailureException(cn.taketoday.dao.DataAccessResourceFailureException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 2 with DataAccessResourceFailureException

use of cn.taketoday.dao.DataAccessResourceFailureException in project today-infrastructure by TAKETODAY.

the class TemporaryLobCreator method setClobAsAsciiStream.

@Override
public void setClobAsAsciiStream(PreparedStatement ps, int paramIndex, @Nullable InputStream asciiStream, int contentLength) throws SQLException {
    if (asciiStream != null) {
        Clob clob = ps.getConnection().createClob();
        try {
            FileCopyUtils.copy(asciiStream, clob.setAsciiStream(1));
        } catch (IOException ex) {
            throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex);
        }
        this.temporaryClobs.add(clob);
        ps.setClob(paramIndex, clob);
    } else {
        ps.setClob(paramIndex, (Clob) null);
    }
    if (logger.isDebugEnabled()) {
        logger.debug(asciiStream != null ? "Copied ASCII stream into temporary CLOB with length " + contentLength : "Set CLOB to null");
    }
}
Also used : DataAccessResourceFailureException(cn.taketoday.dao.DataAccessResourceFailureException) IOException(java.io.IOException) Clob(java.sql.Clob)

Example 3 with DataAccessResourceFailureException

use of cn.taketoday.dao.DataAccessResourceFailureException in project today-infrastructure by TAKETODAY.

the class TemporaryLobCreator method setBlobAsBinaryStream.

@Override
public void setBlobAsBinaryStream(PreparedStatement ps, int paramIndex, @Nullable InputStream binaryStream, int contentLength) throws SQLException {
    if (binaryStream != null) {
        Blob blob = ps.getConnection().createBlob();
        try {
            FileCopyUtils.copy(binaryStream, blob.setBinaryStream(1));
        } catch (IOException ex) {
            throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex);
        }
        this.temporaryBlobs.add(blob);
        ps.setBlob(paramIndex, blob);
    } else {
        ps.setBlob(paramIndex, (Blob) null);
    }
    if (logger.isDebugEnabled()) {
        logger.debug(binaryStream != null ? "Copied binary stream into temporary BLOB with length " + contentLength : "Set BLOB to null");
    }
}
Also used : Blob(java.sql.Blob) DataAccessResourceFailureException(cn.taketoday.dao.DataAccessResourceFailureException) IOException(java.io.IOException)

Example 4 with DataAccessResourceFailureException

use of cn.taketoday.dao.DataAccessResourceFailureException in project today-infrastructure by TAKETODAY.

the class TemporaryLobCreator method setClobAsCharacterStream.

@Override
public void setClobAsCharacterStream(PreparedStatement ps, int paramIndex, @Nullable Reader characterStream, int contentLength) throws SQLException {
    if (characterStream != null) {
        Clob clob = ps.getConnection().createClob();
        try {
            FileCopyUtils.copy(characterStream, clob.setCharacterStream(1));
        } catch (IOException ex) {
            throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex);
        }
        this.temporaryClobs.add(clob);
        ps.setClob(paramIndex, clob);
    } else {
        ps.setClob(paramIndex, (Clob) null);
    }
    if (logger.isDebugEnabled()) {
        logger.debug(characterStream != null ? "Copied character stream into temporary CLOB with length " + contentLength : "Set CLOB to null");
    }
}
Also used : DataAccessResourceFailureException(cn.taketoday.dao.DataAccessResourceFailureException) IOException(java.io.IOException) Clob(java.sql.Clob)

Example 5 with DataAccessResourceFailureException

use of cn.taketoday.dao.DataAccessResourceFailureException in project today-infrastructure by TAKETODAY.

the class AbstractSequenceMaxValueIncrementer method getNextKey.

/**
 * Executes the SQL as specified by {@link #getSequenceQuery()}.
 */
@Override
protected long getNextKey() throws DataAccessException {
    Connection con = DataSourceUtils.getConnection(getDataSource());
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = con.createStatement();
        DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
        rs = stmt.executeQuery(getSequenceQuery());
        if (rs.next()) {
            return rs.getLong(1);
        } else {
            throw new DataAccessResourceFailureException("Sequence query did not return a result");
        }
    } catch (SQLException ex) {
        throw new DataAccessResourceFailureException("Could not obtain sequence value", ex);
    } finally {
        JdbcUtils.closeResultSet(rs);
        JdbcUtils.closeStatement(stmt);
        DataSourceUtils.releaseConnection(con, getDataSource());
    }
}
Also used : DataAccessResourceFailureException(cn.taketoday.dao.DataAccessResourceFailureException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Aggregations

DataAccessResourceFailureException (cn.taketoday.dao.DataAccessResourceFailureException)24 SQLException (java.sql.SQLException)14 Connection (java.sql.Connection)10 IOException (java.io.IOException)6 ResultSet (java.sql.ResultSet)6 Statement (java.sql.Statement)6 EnabledForTestGroups (cn.taketoday.core.testfixture.EnabledForTestGroups)4 UncategorizedSQLException (cn.taketoday.jdbc.UncategorizedSQLException)4 MetaDataAccessException (cn.taketoday.jdbc.support.MetaDataAccessException)4 TransactionStatus (cn.taketoday.transaction.TransactionStatus)4 TransactionTimedOutException (cn.taketoday.transaction.TransactionTimedOutException)4 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)4 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)4 Clob (java.sql.Clob)4 PreparedStatement (java.sql.PreparedStatement)4 Test (org.junit.jupiter.api.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ValueSource (org.junit.jupiter.params.provider.ValueSource)4 InOrder (org.mockito.InOrder)4 ConcurrencyFailureException (cn.taketoday.dao.ConcurrencyFailureException)2