Search in sources :

Example 11 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class CassandraDbProvider method extractObjectFromResultSet.

private static Object extractObjectFromResultSet(Row row, Definition columnDefinition) {
    Object object;
    String columnName = columnDefinition.getName();
    DataType dataType = columnDefinition.getType();
    Name columnTypeName = dataType.getName();
    if (columnTypeName.equals(DataType.Name.UUID)) {
        object = row.getUUID(columnName);
    } else if (columnTypeName.equals(DataType.Name.TIMEUUID)) {
        object = row.getUUID(columnName);
    } else if (columnTypeName.equals(DataType.Name.BOOLEAN)) {
        object = row.getBool(columnName);
    } else if (columnTypeName.equals(DataType.Name.INT)) {
        object = row.getInt(columnName);
    } else if (columnTypeName.equals(DataType.Name.BIGINT)) {
        object = row.getLong(columnName);
    } else if (columnTypeName.equals(DataType.Name.FLOAT)) {
        object = row.getFloat(columnName);
    } else if (columnTypeName.equals(DataType.Name.DOUBLE)) {
        object = row.getDouble(columnName);
    } else if (columnTypeName.equals(DataType.Name.COUNTER)) {
        object = row.getLong(columnName);
    } else if (columnTypeName.equals(DataType.Name.DECIMAL)) {
        object = row.getDecimal(columnName);
    } else if (columnTypeName.equals(DataType.Name.TEXT) || columnTypeName.equals(DataType.Name.VARCHAR)) {
        object = row.getString(columnName);
    } else if (columnTypeName.equals(DataType.Name.TIMESTAMP)) {
        object = row.getDate(columnName);
    } else if (columnTypeName.equals(DataType.Name.BLOB)) {
        ByteBuffer data = (ByteBuffer) row.getBytes(columnName);
        if (data != null) {
            byte[] result = new byte[data.remaining()];
            data.get(result);
            object = result;
        } else {
            object = null;
        }
    } else if (columnTypeName.equals(DataType.Name.SET)) {
        object = row.getSet(columnName, dataType.getTypeArguments().get(0).asJavaClass());
    } else if (columnTypeName.equals(DataType.Name.LIST)) {
        object = row.getList(columnName, dataType.getTypeArguments().get(0).asJavaClass());
    } else if (columnTypeName.equals(DataType.Name.MAP)) {
        object = row.getMap(columnName, dataType.getTypeArguments().get(0).asJavaClass(), dataType.getTypeArguments().get(1).asJavaClass());
    } else {
        throw new DbException("Unsupported data type '" + columnDefinition.getType().toString() + "' for table '" + columnDefinition.getTable() + "' and column '" + columnName + "'");
    }
    return object;
}
Also used : DataType(com.datastax.driver.core.DataType) ByteBuffer(java.nio.ByteBuffer) Name(com.datastax.driver.core.DataType.Name) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 12 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class DbConnSQLServer method getDataSource.

@Override
public DataSource getDataSource() {
    if (jdbcDataSourceClass.getName().equals(DEFAULT_JDBC_DATASOURCE_CLASS_NAME)) {
        // jTDS - default SQL server driver. By default we have it in class path
        // jTDS does not provide connection pool so make one using Apache Commons DBCP
        ds = new BasicDataSource();
        // wait 60 sec for new connection
        ds.setMaxWait(60 * 1000);
        String logAbandoned = System.getProperty("dbcp.logAbandoned");
        if (logAbandoned != null && ("true".equalsIgnoreCase(logAbandoned)) || "1".equalsIgnoreCase(logAbandoned)) {
            log.info("Will log abandoned connections if not cleaned in 120 sec");
            // log not closed connections
            ds.setRemoveAbandoned(true);
            // issue stack trace of not closed connection
            ds.setLogAbandoned(true);
            // 120 sec - 2 min
            ds.setRemoveAbandonedTimeout(120);
        }
        ds.setDriverClassName(getDriverClass().getName());
        ds.setUsername(user);
        ds.setPassword(password);
        ds.setUrl(getURL());
        return ds;
    } else {
        DataSource ds = null;
        try {
            ds = jdbcDataSourceClass.newInstance();
            // FIXME these methods are not standard so error might occur with non-tested driver
            Method setServerName = jdbcDataSourceClass.getMethod("setServerName", String.class);
            setServerName.invoke(ds, this.host);
            Method setPortNumber = jdbcDataSourceClass.getMethod("setPortNumber", int.class);
            setPortNumber.invoke(ds, this.port);
            Method setDatabase = jdbcDataSourceClass.getMethod("setDatabase", String.class);
            setDatabase.invoke(ds, this.db);
        } catch (Exception e) {
            throw new DbException("Error while configuring data source '" + jdbcDataSourceClass.getName() + "' for use", e);
        }
        return ds;
    }
}
Also used : Method(java.lang.reflect.Method) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) DbException(com.axway.ats.core.dbaccess.exceptions.DbException) DataSource(javax.sql.DataSource) BasicDataSource(org.apache.commons.dbcp.BasicDataSource) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 13 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class DbConnSQLServer method updateConnectionSettings.

/**
     * Get latest JDBC connection settings.
     * Alternative for the logdbdriver property is to specify all three system properties:
     *
     * <p>The alternative is to specify manually all three needed properties:</p>
     *
     * <p>JDBC URL prefix system property <strong>com.axway.automation.ats.core.dbaccess.mssql_jdbc_prefix</strong> with possible values:
     * <ul>
     *  <li>"jdbc:jtds:sqlserver://" for jTDS</li>
     *  <li>"jdbc:JSQLConnect://" for JSQLConnect </li>
     * </ul>
     * </p>
     *
     * <p>JDBC driver-related system property <strong>com.axway.automation.ats.core.dbaccess.mssql_jdbc_driver_class</strong> with possible values:
     * <ul>
     *  <li>"net.sourceforge.jtds.jdbc.Driver" for jTDS</li>
     *  <li>"com.jnetdirect.jsql.JSQLDriver" for JSQLConnect </li>
     * </ul>
     * </p>
     *
     * <p>DataSource system property <strong>com.axway.automation.ats.core.dbaccess.mssql_jdbc_datasource_class</strong> with possible values:
     * <ul>
     *  <li>"net.sourceforge.jtds.jdbcx.JtdsDataSource" for jTDS</li>
     *  <li>"com.jnetdirect.jsql.JSQLPoolingDataSource" for JSQLConnect</li>
     * </ul>
     * </p>
     *
     * <p>
     * By default jTDS is used. If you want to use JSQLConnect you should add this
     * to your Java VM invocation command line:<br/>
     * <code>
     * -Dcom.axway.automation.ats.core.dbaccess.mssql_jdbc_prefix=jdbc:JSQLConnect://
     * -Dcom.axway.automation.ats.core.dbaccess.mssql_jdbc_driver_class=com.jnetdirect.jsql.JSQLDriver
     * -Dcom.axway.automation.ats.core.dbaccess.mssql_jdbc_datasource_class=com.jnetdirect.jsql.JSQLPoolingDataSource
     * </code>
     * </p>
     */
private void updateConnectionSettings() {
    String value = System.getProperty(JDBC_DRIVER_VENDOR_KEY);
    // MsSQLJDBCDriverVendor.JTDS; // default version
    MsSQLJDBCDriverVendor vendor = null;
    if (!StringUtils.isNullOrEmpty(value)) {
        value = value.trim().toUpperCase();
        try {
            vendor = MsSQLJDBCDriverVendor.valueOf(value);
        } catch (Exception e) {
            StringBuilder sb = new StringBuilder();
            for (MsSQLJDBCDriverVendor enumValue : MsSQLJDBCDriverVendor.values()) {
                sb.append(enumValue.toString() + ",");
            }
            // remove trailing comma
            sb.delete(sb.length() - 1, sb.length());
            throw new DbException("Illegal value '" + value + "' is specified for Log DB driver. Supported are: " + sb.toString() + ". No DB logging will be performed.", e);
        }
        switch(vendor) {
            case JTDS:
                System.setProperty(JDBC_PREFIX_KEY, DEFAULT_JDBC_DRIVER_PREFIX);
                System.setProperty(JDBC_DRIVER_CLASS_KEY, DEFAULT_JDBC_DRIVER_CLASS_NAME);
                System.setProperty(JDBC_DATASOURCE_CLASS_KEY, DEFAULT_JDBC_DATASOURCE_CLASS_NAME);
                break;
            case JNETDIRECT:
                System.setProperty(JDBC_PREFIX_KEY, JNETDIRECT_JDBC_DRIVER_PREFIX);
                System.setProperty(JDBC_DRIVER_CLASS_KEY, JNETDIRECT_JDBC_DRIVER_CLASS_NAME);
                System.setProperty(JDBC_DATASOURCE_CLASS_KEY, JNETDIRECT_JDBC_DATASOURCE_CLASS_NAME);
                break;
            default:
                // not expected. Just in case if enum is updated w/o implementation here
                throw new DbException("Not implemented support for MsSQL driver type " + vendor.toString());
        }
    }
    // alternative way if JDBC driver is not in enum - the 3 properties can be specified explicitely.
    // The class loading check is also here.
    // JDBC prefix like "jdbc:jtds:sqlserver://" from URL jdbc:jtds:sqlserver://SERVER-NAME:port/DB-NAME
    value = System.getProperty(JDBC_PREFIX_KEY);
    if (value != null) {
        jdbcDriverPrefix = value.trim();
    }
    if (StringUtils.isNullOrEmpty(jdbcDriverPrefix)) {
        // including empty string after trim
        jdbcDriverPrefix = DEFAULT_JDBC_DRIVER_PREFIX;
    }
    log.debug("MsSQL connection: Using JDBC driver prefix: " + jdbcDriverPrefix);
    // JDBC driver class
    value = System.getProperty(JDBC_DRIVER_CLASS_KEY);
    String className = null;
    if (value != null) {
        className = value.trim();
    }
    if (StringUtils.isNullOrEmpty(className)) {
        // including empty string after trim
        className = DEFAULT_JDBC_DRIVER_CLASS_NAME;
    }
    // check for availability
    boolean isClassLoaded = loadClass(className);
    if (isClassLoaded) {
        try {
            jdbcDriverClass = (Class<? extends Driver>) Class.forName(className);
        } catch (ClassNotFoundException e) {
            // Not expected. Already checked in loadClass()
            log.error(null, e);
        } catch (ClassCastException e) {
            throw new DbException("Class with name '" + className + "' is not a valid java.sql.Driver class");
        }
        log.debug("Using JDBC driver class: " + className);
    } else {
        throw new DbException("Could not load MsSQL JDBC driver class with name '" + className + "'");
    }
    // JDBC DataSource class
    value = System.getProperty(JDBC_DATASOURCE_CLASS_KEY);
    className = null;
    if (value != null) {
        className = value.trim();
    }
    if (StringUtils.isNullOrEmpty(className)) {
        // including empty string after trim
        className = DEFAULT_JDBC_DATASOURCE_CLASS_NAME;
    }
    // check for availability
    isClassLoaded = loadClass(className);
    if (isClassLoaded) {
        try {
            jdbcDataSourceClass = (Class<? extends DataSource>) Class.forName(className);
        } catch (ClassNotFoundException e) {
            // Not expected. Already checked in loadClass()
            log.error(null, e);
        } catch (ClassCastException e) {
            throw new DbException("Class with name '" + className + "' is not a valid javax.sql.DataSource class");
        }
        log.debug("Using JDBC data source class: " + className);
    } else {
        throw new DbException("Could not load MsSQL JDBC DataSource class with name '" + className + "'");
    }
}
Also used : DbException(com.axway.ats.core.dbaccess.exceptions.DbException) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 14 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class MysqlEnvironmentHandler method checkDriverVersion.

private boolean checkDriverVersion(MysqlDbProvider dbProvider) {
    try {
        Connection connection = dbProvider.getConnection();
        DatabaseMetaData dmd = connection.getMetaData();
        int majorVersion = dmd.getDriverMajorVersion();
        int minorVersion = dmd.getDriverMinorVersion();
        log.info(new StringBuilder().append("JDBC driver used is : ").append(majorVersion).append(".").append(minorVersion).toString());
        // The older specification is used in drivers prior to 5.0 including 5.0
        if (majorVersion < 5) {
            return false;
        } else if (majorVersion == 5 && minorVersion == 0) {
            return false;
        }
    } catch (SQLException e) {
        log.error("Unable to determine driver version, falling back to JDBC3 specs", e);
        return false;
    } catch (DbException e) {
        log.error("Unable to determine driver version, falling back to JDBC3 specs", e);
        return false;
    }
    return true;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Example 15 with DbException

use of com.axway.ats.core.dbaccess.exceptions.DbException in project ats-framework by Axway.

the class OracleEnvironmentHandler method getColumnsToSelect.

@Override
protected List<ColumnDescription> getColumnsToSelect(DbTable table, String userName) throws DbException, ColumnHasNoDefaultValueException {
    // TODO Implementation might be replaced with JDBC DatabaseMetaData.getColumns() but should be verified
    // with default column values
    // ALL_TAB_COLS - All columns of tables accessible by this user. OWNER restriction is used because user might
    // have access to other user's tables and columns
    String selectColumnsInfo = "SELECT * FROM ALL_TAB_COLS WHERE TABLE_NAME='" + table.getTableName().toUpperCase() + "' AND OWNER='" + userName.toUpperCase() + "'";
    ArrayList<ColumnDescription> columnsToSelect = new ArrayList<ColumnDescription>();
    DbRecordValuesList[] columnsMetaData = null;
    try {
        columnsMetaData = this.dbProvider.select(selectColumnsInfo);
    } catch (DbException e) {
        log.error("Could not get columns for table " + table.getTableName() + ". You may check if the table exists, if the you are using the right user and it has the right permissions. See more details in the trace.");
        throw e;
    }
    if (columnsMetaData.length == 0) {
        throw new DbException("Could not get columns for table " + table.getTableName() + ". You may check if the table exists, if the you are using the right user and it has the right permissions.");
    }
    for (DbRecordValuesList columnMetaData : columnsMetaData) {
        String columnName = (String) columnMetaData.get("COLUMN_NAME");
        //check if the column should be skipped in the backup
        if (!table.getColumnsToExclude().contains(columnName)) {
            ColumnDescription colDescription = new OracleColumnDescription(columnName, (String) columnMetaData.get("DATA_TYPE"));
            columnsToSelect.add(colDescription);
        } else {
            //if this column has no default value, we cannot skip it in the backup
            if (columnMetaData.get("DATA_DEFAULT") == null) {
                log.error("Cannot skip columns with no default values while creating backup");
                throw new ColumnHasNoDefaultValueException(table.getTableName(), columnName);
            }
        }
    }
    return columnsToSelect;
}
Also used : DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) ColumnHasNoDefaultValueException(com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException) OracleColumnDescription(com.axway.ats.core.dbaccess.OracleColumnDescription) ColumnDescription(com.axway.ats.core.dbaccess.ColumnDescription) ArrayList(java.util.ArrayList) OracleColumnDescription(com.axway.ats.core.dbaccess.OracleColumnDescription) DbException(com.axway.ats.core.dbaccess.exceptions.DbException)

Aggregations

DbException (com.axway.ats.core.dbaccess.exceptions.DbException)31 SQLException (java.sql.SQLException)11 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)8 DatabaseOperationsException (com.axway.ats.action.exceptions.DatabaseOperationsException)7 PublicAtsApi (com.axway.ats.common.PublicAtsApi)7 Connection (java.sql.Connection)6 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 PreparedStatement (java.sql.PreparedStatement)5 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)4 DatabaseEnvironmentCleanupException (com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException)4 BufferedReader (java.io.BufferedReader)4 File (java.io.File)4 FileReader (java.io.FileReader)4 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)3 CassandraDbProvider (com.axway.ats.core.dbaccess.cassandra.CassandraDbProvider)3 ColumnHasNoDefaultValueException (com.axway.ats.environment.database.exceptions.ColumnHasNoDefaultValueException)3 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 DatabaseCell (com.axway.ats.action.dbaccess.model.DatabaseCell)2