Пример #1
0
 protected void executeQuery(final Statement statement, final String sql) throws SQLException {
   try {
     long start = 0;
     if (LOG.isDebugEnabled()) {
       start = System.currentTimeMillis();
       LOG.debug("Execute script: \n[" + sql + "]");
     }
     SecurityHelper.doPrivilegedSQLExceptionAction(
         new PrivilegedExceptionAction<Object>() {
           public Object run() throws Exception {
             statement.executeUpdate(sql);
             return null;
           }
         });
     if (LOG.isDebugEnabled()) {
       LOG.debug(
           "Script "
               + sql
               + " executed in "
               + ((System.currentTimeMillis() - start) / 1000d)
               + " sec");
     }
   } catch (SQLException e) {
     LOG.error("Query execution \"" + sql + "\" failed: " + JDBCUtils.getFullMessage(e), e);
     throw e;
   }
 }
Пример #2
0
  /** Opens connection to database underlying a workspace. */
  private static Connection getConnection(WorkspaceEntry wsEntry) throws DBCleanException {
    String dsName = getSourceNameParameter(wsEntry);

    DataSource ds;
    try {
      ds = (DataSource) new InitialContext().lookup(dsName);
    } catch (NamingException e) {
      throw new DBCleanException(e);
    }

    if (ds == null) {
      throw new DBCleanException("Data source " + dsName + " not found");
    }

    final DataSource dsF = ds;

    Connection jdbcConn;
    try {
      jdbcConn =
          SecurityHelper.doPrivilegedSQLExceptionAction(
              new PrivilegedExceptionAction<Connection>() {
                public Connection run() throws Exception {
                  return dsF.getConnection();
                }
              });
    } catch (SQLException e) {
      throw new DBCleanException(e);
    }

    return jdbcConn;
  }
Пример #3
0
  /**
   * Detect databse dialect using JDBC metadata. Based on code of
   * http://svn.jboss.org/repos/hibernate/core/trunk/core/src/main/java/org/hibernate/
   * dialect/resolver/StandardDialectResolver.java
   *
   * @param metaData {@link DatabaseMetaData}
   * @return String
   * @throws SQLException if error occurs
   */
  public static String detect(final DatabaseMetaData metaData) throws SQLException {
    final String databaseName =
        SecurityHelper.doPrivilegedSQLExceptionAction(
            new PrivilegedExceptionAction<String>() {
              public String run() throws Exception {
                return metaData.getDatabaseProductName();
              }
            });

    if ("HSQL Database Engine".equals(databaseName)) {
      return DialectConstants.DB_DIALECT_HSQLDB;
    }

    if ("H2".equals(databaseName)) {
      return DialectConstants.DB_DIALECT_H2;
    }

    if ("MySQL".equals(databaseName)) {
      return DialectConstants.DB_DIALECT_MYSQL;
    }

    if ("PostgreSQL".equals(databaseName)) {
      int majorVersion = metaData.getDatabaseMajorVersion();
      int minorVersion = metaData.getDatabaseMinorVersion();

      return (majorVersion > 9 || (majorVersion == 9 && minorVersion >= 1))
          ? DialectConstants.DB_DIALECT_PGSQL_SCS
          : DialectConstants.DB_DIALECT_PGSQL;
    }

    if ("Apache Derby".equals(databaseName)) {
      return DialectConstants.DB_DIALECT_DERBY;
    }

    if ("ingres".equalsIgnoreCase(databaseName)) {
      return DialectConstants.DB_DIALECT_INGRES;
    }

    if (databaseName.startsWith("Microsoft SQL Server")) {
      return DialectConstants.DB_DIALECT_MSSQL;
    }

    if ("Sybase SQL Server".equals(databaseName)
        || "Adaptive Server Enterprise".equals(databaseName)) {
      return DialectConstants.DB_DIALECT_SYBASE;
    }

    if (databaseName.startsWith("Adaptive Server Anywhere")) {
      return DialectConstants.DB_DIALECT_SYBASE;
    }

    if (databaseName.startsWith("DB2/")) {
      return detectDB2Dialect(metaData);
    }

    if ("Oracle".equals(databaseName)) {
      return DialectConstants.DB_DIALECT_ORACLE;
    }

    return DialectConstants.DB_DIALECT_GENERIC;
  }