Search in sources :

Example 16 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.

the class SimulatorManagerImpl method configure.

@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
    try {
        Connection conn = Transaction.getStandaloneConnectionWithException();
        conn.setAutoCommit(true);
        _concierge = new ConnectionConcierge("SimulatorConnection", conn, true);
    } catch (SQLException e) {
        throw new CloudRuntimeException("Unable to get a db connection", e);
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(java.sql.Connection) ConnectionConcierge(com.cloud.utils.db.ConnectionConcierge)

Example 17 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.

the class GenericDaoBase method executeList.

protected List<T> executeList(final String sql, final Object... params) {
    final Transaction txn = Transaction.currentTxn();
    PreparedStatement pstmt = null;
    final List<T> result = new ArrayList<T>();
    try {
        pstmt = txn.prepareAutoCloseStatement(sql);
        int i = 0;
        for (final Object param : params) {
            pstmt.setObject(++i, param);
        }
        final ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            result.add(toEntityBean(rs, true));
        }
        return result;
    } catch (final SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + pstmt, e);
    } catch (final Throwable e) {
        throw new CloudRuntimeException("Caught: " + pstmt, e);
    }
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 18 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.

the class GenericDaoBase method expunge.

@Override
public boolean expunge(final ID id) {
    final Transaction txn = Transaction.currentTxn();
    PreparedStatement pstmt = null;
    String sql = null;
    try {
        txn.start();
        for (final Pair<String, Attribute[]> deletSql : _deleteSqls) {
            sql = deletSql.first();
            final Attribute[] attrs = deletSql.second();
            pstmt = txn.prepareAutoCloseStatement(sql);
            for (int i = 0; i < attrs.length; i++) {
                prepareAttribute(i + 1, pstmt, attrs[i], id);
            }
            pstmt.executeUpdate();
        }
        txn.commit();
        if (_cache != null) {
            _cache.remove(id);
        }
        return true;
    } catch (final SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + pstmt, e);
    }
}
Also used : SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PreparedStatement(java.sql.PreparedStatement) AttributeOverride(javax.persistence.AttributeOverride)

Example 19 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.

the class GenericDaoBase method searchIncludingRemoved.

@Override
public List<T> searchIncludingRemoved(SearchCriteria<T> sc, final Filter filter, final Boolean lock, final boolean cache, final boolean enable_query_cache) {
    String clause = sc != null ? sc.getWhereClause() : null;
    if (clause != null && clause.length() == 0) {
        clause = null;
    }
    final StringBuilder str = createPartialSelectSql(sc, clause != null, enable_query_cache);
    if (clause != null) {
        str.append(clause);
    }
    Collection<JoinBuilder<SearchCriteria<?>>> joins = null;
    if (sc != null) {
        joins = sc.getJoins();
        if (joins != null) {
            addJoins(str, joins);
        }
    }
    List<Object> groupByValues = addGroupBy(str, sc);
    addFilter(str, filter);
    final Transaction txn = Transaction.currentTxn();
    if (lock != null) {
        assert (txn.dbTxnStarted() == true) : "As nice as I can here now....how do you lock when there's no DB transaction?  Review your db 101 course from college.";
        str.append(lock ? FOR_UPDATE_CLAUSE : SHARE_MODE_CLAUSE);
    }
    final String sql = str.toString();
    PreparedStatement pstmt = null;
    final List<T> result = new ArrayList<T>();
    try {
        pstmt = txn.prepareAutoCloseStatement(sql);
        int i = 0;
        if (clause != null) {
            for (final Pair<Attribute, Object> value : sc.getValues()) {
                prepareAttribute(++i, pstmt, value.first(), value.second());
            }
        }
        if (joins != null) {
            i = addJoinAttributes(i, pstmt, joins);
        }
        if (groupByValues != null) {
            for (Object value : groupByValues) {
                pstmt.setObject(i++, value);
            }
        }
        if (s_logger.isDebugEnabled() && lock != null) {
            txn.registerLock(pstmt.toString());
        }
        final ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            result.add(toEntityBean(rs, cache));
        }
        return result;
    } catch (final SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + pstmt, e);
    } catch (final Throwable e) {
        throw new CloudRuntimeException("Caught: " + pstmt, e);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) AttributeOverride(javax.persistence.AttributeOverride)

Example 20 with CloudRuntimeException

use of com.cloud.utils.exception.CloudRuntimeException in project CloudStack-archive by CloudStack-extras.

the class GenericDaoBase method setField.

@DB(txn = false)
protected void setField(Object entity, Field field, ResultSet rs, int index) throws SQLException {
    try {
        final Class<?> type = field.getType();
        if (type == String.class) {
            byte[] bytes = rs.getBytes(index);
            if (bytes != null) {
                try {
                    if (field.getAnnotation(Column.class).encryptable()) {
                        field.set(entity, DBEncryptionUtil.decrypt(new String(bytes, "UTF-8")));
                    } else {
                        field.set(entity, new String(bytes, "UTF-8"));
                    }
                } catch (IllegalArgumentException e) {
                    assert (false);
                    throw new CloudRuntimeException("IllegalArgumentException when converting UTF-8 data");
                } catch (UnsupportedEncodingException e) {
                    assert (false);
                    throw new CloudRuntimeException("UnsupportedEncodingException when converting UTF-8 data");
                }
            } else {
                field.set(entity, null);
            }
        } else if (type == long.class) {
            field.setLong(entity, rs.getLong(index));
        } else if (type == Long.class) {
            if (rs.getObject(index) == null) {
                field.set(entity, null);
            } else {
                field.set(entity, rs.getLong(index));
            }
        } else if (type.isEnum()) {
            final Enumerated enumerated = field.getAnnotation(Enumerated.class);
            final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
            final Enum<?>[] enums = (Enum<?>[]) field.getType().getEnumConstants();
            for (final Enum<?> e : enums) {
                if ((enumType == EnumType.STRING && e.name().equalsIgnoreCase(rs.getString(index))) || (enumType == EnumType.ORDINAL && e.ordinal() == rs.getInt(index))) {
                    field.set(entity, e);
                    return;
                }
            }
        } else if (type == int.class) {
            field.set(entity, rs.getInt(index));
        } else if (type == Integer.class) {
            if (rs.getObject(index) == null) {
                field.set(entity, null);
            } else {
                field.set(entity, rs.getInt(index));
            }
        } else if (type == Date.class) {
            final Object data = rs.getDate(index);
            if (data == null) {
                field.set(entity, null);
                return;
            }
            field.set(entity, DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
        } else if (type == Calendar.class) {
            final Object data = rs.getDate(index);
            if (data == null) {
                field.set(entity, null);
                return;
            }
            final Calendar cal = Calendar.getInstance();
            cal.setTime(DateUtil.parseDateString(s_gmtTimeZone, rs.getString(index)));
            field.set(entity, cal);
        } else if (type == boolean.class) {
            field.setBoolean(entity, rs.getBoolean(index));
        } else if (type == Boolean.class) {
            if (rs.getObject(index) == null) {
                field.set(entity, null);
            } else {
                field.set(entity, rs.getBoolean(index));
            }
        } else if (type == URI.class) {
            try {
                String str = rs.getString(index);
                field.set(entity, str == null ? null : new URI(str));
            } catch (URISyntaxException e) {
                throw new CloudRuntimeException("Invalid URI: " + rs.getString(index), e);
            }
        } else if (type == URL.class) {
            try {
                String str = rs.getString(index);
                field.set(entity, str != null ? new URL(str) : null);
            } catch (MalformedURLException e) {
                throw new CloudRuntimeException("Invalid URL: " + rs.getString(index), e);
            }
        } else if (type == Ip.class) {
            final Enumerated enumerated = field.getAnnotation(Enumerated.class);
            final EnumType enumType = (enumerated == null) ? EnumType.STRING : enumerated.value();
            Ip ip = null;
            if (enumType == EnumType.STRING) {
                String s = rs.getString(index);
                ip = s == null ? null : new Ip(NetUtils.ip2Long(s));
            } else {
                ip = new Ip(rs.getLong(index));
            }
            field.set(entity, ip);
        } else if (type == short.class) {
            field.setShort(entity, rs.getShort(index));
        } else if (type == Short.class) {
            if (rs.getObject(index) == null) {
                field.set(entity, null);
            } else {
                field.set(entity, rs.getShort(index));
            }
        } else if (type == float.class) {
            field.setFloat(entity, rs.getFloat(index));
        } else if (type == Float.class) {
            if (rs.getObject(index) == null) {
                field.set(entity, null);
            } else {
                field.set(entity, rs.getFloat(index));
            }
        } else if (type == double.class) {
            field.setDouble(entity, rs.getDouble(index));
        } else if (type == Double.class) {
            if (rs.getObject(index) == null) {
                field.set(entity, null);
            } else {
                field.set(entity, rs.getDouble(index));
            }
        } else if (type == byte.class) {
            field.setByte(entity, rs.getByte(index));
        } else if (type == Byte.class) {
            if (rs.getObject(index) == null) {
                field.set(entity, null);
            } else {
                field.set(entity, rs.getByte(index));
            }
        } else if (type == byte[].class) {
            field.set(entity, rs.getBytes(index));
        } else {
            field.set(entity, rs.getObject(index));
        }
    } catch (final IllegalAccessException e) {
        throw new CloudRuntimeException("Yikes! ", e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Calendar(java.util.Calendar) Ip(com.cloud.utils.net.Ip) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) Enumerated(javax.persistence.Enumerated) Column(javax.persistence.Column) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) EnumType(javax.persistence.EnumType)

Aggregations

CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1279 PreparedStatement (java.sql.PreparedStatement)320 SQLException (java.sql.SQLException)320 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)236 ResultSet (java.sql.ResultSet)217 ArrayList (java.util.ArrayList)217 ConfigurationException (javax.naming.ConfigurationException)171 HashMap (java.util.HashMap)129 DB (com.cloud.utils.db.DB)118 TransactionLegacy (com.cloud.utils.db.TransactionLegacy)115 IOException (java.io.IOException)95 HostVO (com.cloud.host.HostVO)94 Answer (com.cloud.agent.api.Answer)84 Account (com.cloud.user.Account)84 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)82 URISyntaxException (java.net.URISyntaxException)82 ActionEvent (com.cloud.event.ActionEvent)70 TransactionStatus (com.cloud.utils.db.TransactionStatus)65 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)63 InternalErrorException (com.cloud.exception.InternalErrorException)57