public static void runTest3() throws Exception {
   boolean exceptionOccured = false;
   try {
     Context ctx = cache.getJNDIContext();
     DataSource ds1 = null;
     DataSource ds2 = null;
     ds1 = (DataSource) ctx.lookup("java:/XAPooledDataSource");
     ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
     ds2.getConnection();
     UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
     utx.begin();
     utx.setTransactionTimeout(2);
     ds1.getConnection();
     Thread.sleep(4000);
     try {
       utx.commit();
     } catch (Exception e) {
       exceptionOccured = true;
     }
     if (!exceptionOccured)
       fail("Exception (Transaction-Time-Out)did not occur although was supposed" + "to occur");
   } catch (Exception e) {
     fail("failed in runTest3 due to " + e);
   }
 }
 public static void runTest1() throws Exception {
   boolean exceptionOccured = false;
   try {
     Context ctx = cache.getJNDIContext();
     DataSource ds1 = null;
     DataSource ds2 = null;
     ds1 = (DataSource) ctx.lookup("java:/XAPooledDataSource");
     ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
     ds2.getConnection();
     ds1 = (DataSource) ctx.lookup("java:/XAPooledDataSource");
     UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
     utx.begin();
     ds1.getConnection();
     Thread.sleep(8000);
     try {
       utx.commit();
     } catch (Exception e) {
       exceptionOccured = true;
     }
     if (!exceptionOccured)
       fail("Exception did not occur on commit although was supposed" + "occur");
   } catch (Exception e) {
     getLogWriter().fine("Exception caught in runTest1 due to : " + e);
     fail("failed in runTest1 due to " + e);
   }
 }
  //
  // Find all the objects inside the DataSource and vet them.
  //
  private void vetDataSource(HashSet<String> unsupportedList, HashSet<String> notUnderstoodList)
      throws Exception {
    DataSource ds = JDBCDataSource.getDataSource();
    Connection conn = ds.getConnection();

    vetObject(ds, unsupportedList, notUnderstoodList);

    connectionWorkhorse(conn, unsupportedList, notUnderstoodList);
  }
예제 #4
0
 public void run() {
   Connection con = null;
   Statement stmt = null;
   try {
     DataSource ds = getDataSource();
     con = ds.getConnection();
     if (executeOnlyIf(con, onlyIfQuery)) {
       stmt = con.createStatement();
       if (query != null) {
         executeQuery(stmt, query);
       } else if (update != null) {
         executeUpdate(stmt, update);
       } else {
         throw new IllegalStateException("Both query and update properties are unset");
       }
     } else {
       LOG.debug("Skipped because of " + onlyIfQuery);
     }
   } catch (RuntimeException e) {
     throw e;
   } catch (Throwable t) {
     if (ignore.matcher(t.getMessage()).matches()) {
       LOG.info("Ignoring " + t.getMessage());
     } else {
       throw new RuntimeException(t.getMessage(), t);
     }
   } finally {
     try {
       if (stmt != null) {
         stmt.close();
       }
     } catch (Exception g) {
     }
     try {
       if (con != null) {
         con.close();
       }
     } catch (Exception g) {
     }
   }
 }
 private Connection getConnection() throws SQLException {
   DataSource dataSource = null;
   try {
     dataSource = DataSource.getInstance();
   } catch (IOException e) {
     LOG.error("IOException : " + e);
   } catch (SQLException e) {
     LOG.error("SQLException : " + e);
   } catch (PropertyVetoException e) {
     LOG.error("PropertyVetoException : " + e);
   }
   return dataSource.getConnection();
 }
 public static void runTest2() throws Exception {
   boolean exceptionOccured1 = false;
   boolean exceptionOccured2 = false;
   try {
     Context ctx = cache.getJNDIContext();
     DataSource ds1 = null;
     DataSource ds2 = null;
     ds1 = (DataSource) ctx.lookup("java:/XAPooledDataSource");
     ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
     ds2.getConnection();
     ds1.getConnection();
     ds1.getConnection();
     ds1.getConnection();
     ds1.getConnection();
     ds1.getConnection();
     UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
     utx.begin();
     try {
       ds1.getConnection();
       Thread.sleep(8000);
     } catch (SQLException e) {
       exceptionOccured1 = true;
     }
     try {
       utx.commit();
     } catch (Exception e) {
       exceptionOccured2 = true;
     }
     if (!exceptionOccured1)
       fail("Exception (Login-Time-Out)did not occur although was supposed" + "to occur");
     if (exceptionOccured2)
       fail("Exception did occur on commit, although was not supposed" + "to occur");
   } catch (Exception e) {
     fail("failed in runTest2 due to " + e);
   }
 }
예제 #7
0
  /** Constructor for LDAPQuery. */
  public JNDITomcat() throws Exception {
    super();

    Hashtable<String, String> env = new Hashtable<String, String>();

    env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    env.put(Context.PROVIDER_URL, "jndi://nadetrou2:8080/");

    // Obtain our environment naming context

    Context initCtx = new InitialContext(env);

    // Context envCtx = (Context) initCtx.lookup("comp/env");

    // Look up our data source

    // DataSource datasource = (DataSource)
    // initialContext.lookup((String)settings.get(CONNECTION_POOL_NAME));

    DataSource ds = (DataSource) initCtx.lookup("/jdbc/vrdb");

    // Allocate and use a connection from the pool

    Connection conn = ds.getConnection();

    System.out.println(conn);
    // ... use this connection to access the database ...

    conn.close();

    // Hashtable env = new Hashtable();
    // //env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    // //env.put(Context.PROVIDER_URL, "t3://192.168.0.55:17001/" );
    //
    // env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
    // env.put(Context.PROVIDER_URL, "3://127.0.0.1:7001/" );
    //
    // // env.put(Context.SECURITY_PRINCIPAL, "system");
    // // env.put(Context.SECURITY_CREDENTIALS, "12345678");
    // System.out.println(env);
    // //System.out.println("--A");
    // // Create initial context
    // DirContext ctx = new InitialDirContext(env);

    // exploreNext(0, envCtx, "");
  }
예제 #8
0
 public Customer updateCustomer(Customer customer) {
   Connection connection = null;
   CallableStatement callableStatement = null;
   ResultSet resultSet = null;
   boolean hasResults;
   String id;
   try {
     connection = dataSource.getConnection(); // get connection from dataSource
     callableStatement = connection.prepareCall(updateCustomerSql); // prepare callable statement
     id = customer.getId();
     if (id == null) {
       callableStatement.setNull(1, Types.INTEGER);
     } else {
       callableStatement.setInt(1, Integer.parseInt(id));
     }
     if (customer.isInactive()) {
       callableStatement.setNull(2, Types.VARCHAR);
       callableStatement.setNull(3, Types.VARCHAR);
       callableStatement.setNull(4, Types.VARCHAR);
       callableStatement.setNull(5, Types.VARCHAR);
       callableStatement.setNull(6, Types.VARCHAR);
       callableStatement.setNull(7, Types.VARCHAR);
       callableStatement.setNull(8, Types.VARCHAR);
       callableStatement.setNull(9, Types.VARCHAR);
       callableStatement.setNull(10, Types.VARCHAR);
       callableStatement.setNull(11, Types.VARCHAR);
       callableStatement.setByte(12, (byte) 1);
     } else {
       callableStatement.setString(2, customer.getFirstName());
       callableStatement.setString(3, customer.getLastName());
       callableStatement.setString(4, customer.getStreetAddress());
       callableStatement.setString(5, customer.getAptAddress());
       callableStatement.setString(6, customer.getCity());
       callableStatement.setString(7, customer.getState());
       callableStatement.setString(8, customer.getZip());
       callableStatement.setString(9, customer.getPhone());
       callableStatement.setString(10, customer.getEmail());
       callableStatement.setString(11, customer.getNotes());
       callableStatement.setByte(12, (byte) 0);
     }
     hasResults = callableStatement.execute();
     if (hasResults) {
       resultSet = callableStatement.getResultSet();
       if (resultSet.next()) {
         customer.setId(resultSet.getString(1));
       } else {
         throw new SQLException("Unable to update customer.");
       }
     } else {
       throw new SQLException("Unable to update customer.");
     }
   } catch (SQLException se) {
     log.error("SQL error: ", se);
     return null;
   } finally {
     try {
       resultSet.close();
     } catch (Exception se) {
       log.error("Unable to close resultSet: ", se);
     }
     try {
       callableStatement.close();
     } catch (Exception se) {
       log.error("Unable to close callableStatement: ", se);
     }
     try {
       connection.close();
     } catch (Exception se) {
       log.error("Unable to close connection: ", se);
     }
   }
   return customer;
 }
예제 #9
0
 public List<Customer> findCustomers(String id, String match, int limit) {
   Connection connection = null;
   CallableStatement callableStatement = null;
   ResultSet resultSet = null;
   boolean hasResults;
   List<Customer> customers = new ArrayList<Customer>();
   try {
     connection = dataSource.getConnection(); // get connection from dataSource
     connection.setTransactionIsolation(
         Connection.TRANSACTION_READ_COMMITTED); // prevent dirty reads
     callableStatement = connection.prepareCall(listCustomersSql); // prepare callable statement
     if (id == null) {
       callableStatement.setNull(1, Types.INTEGER);
     } else {
       callableStatement.setInt(1, Integer.parseInt(id));
     }
     if (match == null) {
       callableStatement.setNull(2, Types.VARCHAR);
     } else {
       callableStatement.setString(2, match);
     }
     callableStatement.setInt(3, limit);
     callableStatement.setNull(4, Types.INTEGER);
     hasResults = callableStatement.execute();
     if (hasResults) {
       resultSet = callableStatement.getResultSet();
       if (resultSet.isBeforeFirst()) { // customers have been returned
         while (resultSet.next()) {
           customers.add(
               new Customer(
                   resultSet.getString("id"),
                   resultSet.getString("first_name"),
                   resultSet.getString("last_name"),
                   resultSet.getString("street_address"),
                   resultSet.getString("apt_address"),
                   resultSet.getString("city"),
                   resultSet.getString("state"),
                   resultSet.getString("zip"),
                   resultSet.getString("phone"),
                   resultSet.getString("email"),
                   resultSet.getString("notes")));
         }
       } else {
         log.debug("No customers returned.");
       }
     } else {
       log.debug("No customers returned.");
     }
   } catch (SQLException se) {
     log.error("SQL error: ", se);
     return null;
   } finally {
     try {
       resultSet.close();
     } catch (Exception se) {
       log.error("Unable to close resultSet: ", se);
     }
     try {
       callableStatement.close();
     } catch (Exception se) {
       log.error("Unable to close callableStatement: ", se);
     }
     try {
       connection.close();
     } catch (Exception se) {
       log.error("Unable to close connection: ", se);
     }
   }
   return customers;
 }
  // implement FarragoMedDataServer
  public FarragoMedColumnSet newColumnSet(
      String[] localName,
      Properties tableProps,
      FarragoTypeFactory typeFactory,
      RelDataType rowType,
      Map<String, Properties> columnPropMap)
      throws SQLException {
    if (rowType == null) {
      rowType = createMockRowType(typeFactory);
    }

    assert (rowType.getFieldList().size() == 1);
    RelDataType type = rowType.getFields()[0].getType();
    assert (!type.isNullable());
    assert (typeFactory.getClassForPrimitive(type) != null);

    // TODO jvs 5-Aug-2005:  clean up usage of server properties
    // as defaults

    long nRows = -1;
    String rowCountSql = tableProps.getProperty(PROP_ROW_COUNT_SQL);
    if (rowCountSql != null) {
      // Attempt to issue a loopback query into Farrago to
      // get the number of rows to produce.
      DataSource loopbackDataSource = getLoopbackDataSource();
      Connection connection = null;
      if (loopbackDataSource != null) {
        try {
          connection = loopbackDataSource.getConnection();
          Statement stmt = connection.createStatement();
          ResultSet resultSet = stmt.executeQuery(rowCountSql);
          if (resultSet.next()) {
            nRows = resultSet.getLong(1);
          }
        } finally {
          // It's OK not to clean up stmt and resultSet;
          // connection.close() will do that for us.
          if (connection != null) {
            connection.close();
          }
        }
      }
    }

    if (nRows == -1) {
      nRows =
          getLongProperty(
              tableProps, PROP_ROW_COUNT, getLongProperty(getProperties(), PROP_ROW_COUNT, 10));
    }

    String executorImpl =
        tableProps.getProperty(
            PROP_EXECUTOR_IMPL, getProperties().getProperty(PROP_EXECUTOR_IMPL, PROPVAL_JAVA));
    assert (executorImpl.equals(PROPVAL_JAVA) || executorImpl.equals(PROPVAL_FENNEL));

    String udxSpecificName = tableProps.getProperty(PROP_UDX_SPECIFIC_NAME);

    if (udxSpecificName != null) {
      assert (executorImpl.equals(PROPVAL_JAVA));
    }

    checkNameMatch(getForeignSchemaName(), tableProps.getProperty(PROP_SCHEMA_NAME));

    checkNameMatch(getForeignTableName(), tableProps.getProperty(PROP_TABLE_NAME));

    return new MedMockColumnSet(this, localName, rowType, nRows, executorImpl, udxSpecificName);
  }