Package jp.co.ntt.oss.data

Examples of jp.co.ntt.oss.data.DatabaseResource


    if (showHelp) {
      showHelp();
      return;
    }

    DatabaseResource masterDB = null;
    Connection masterConn = null;
    UserTransaction utx = null;

    try {
      // get master server connection
      masterDB = new DatabaseResource(master);
      masterConn = masterDB.getConnection();

      // begin transaction
      utx = masterDB.getUserTransaction();
      utx.begin();

      // create
      SyncDatabaseDAO.createMlog(masterConn, schema, table);

      // commit transaction
      utx.commit();

    } catch (final Exception e) {
      // rollback transaction
      if (utx != null && utx.getStatus() != Status.STATUS_NO_TRANSACTION) {
        utx.rollback();
      }

      throw e;
    } finally {
      // release resources
      if (masterConn != null) {
        masterConn.close();
      }
      if (masterDB != null) {
        masterDB.stop();
      }
    }

    log.info(mProperty.getMessage("info.create.success"));
  }
View Full Code Here


      SQLException {
    if (master == null) {
      throw new SyncDatabaseException("error.argument");
    }

    DatabaseResource db = null;
    Connection conn = null;

    try {
      // get replica server connection
      db = new DatabaseResource(master);
      conn = db.getConnection();

      final ArrayList<MasterStatus> masters = SyncDatabaseDAO
          .getMasterStatus(conn, schema, table);

      final StringBuilder status = new StringBuilder();
      status.append("master status\n");

      // get display size
      final int[] widths = new int[MasterStatus.MASTERSTATUS_COLUMNS];
      for (int i = 0; i < MasterStatus.MASTERSTATUS_COLUMNS; i++) {
        widths[i] = MasterStatus.HEADERS[i].length();
      }

      widths[MASTERSTATUS_OLDEST_REFRESH] = TIMESTAMP_WIDTH;
      for (final MasterStatus masterStatus : masters) {
        if (masterStatus.getSchema() != null) {
          widths[MASTERSTATUS_SCHEMA] = Math.max(
              widths[MASTERSTATUS_SCHEMA], masterStatus
                  .getSchema().getBytes().length);
        }

        if (masterStatus.getTable() != null) {
          widths[MASTERSTATUS_TABLE] = Math.max(
              widths[MASTERSTATUS_TABLE], masterStatus.getTable()
                  .getBytes().length);
        }

        widths[MASTERSTATUS_LOGS] = Math.max(widths[MASTERSTATUS_LOGS],
            Long.toString(masterStatus.getLogCount()).length());

        widths[MASTERSTATUS_SUBS] = Math.max(widths[MASTERSTATUS_SUBS],
            Long.toString(masterStatus.getSubscribers()).length());

        if (masterStatus.getOldestReplica() != null) {
          widths[MASTERSTATUS_OLDEST_REPLICA] = Math.max(
              widths[MASTERSTATUS_OLDEST_REPLICA], masterStatus
                  .getOldestReplica().getBytes().length);
        }
      }

      // append header
      int i;
      for (i = 0; i < MasterStatus.MASTERSTATUS_COLUMNS - 1; i++) {
        appendStringValue(status, MasterStatus.HEADERS[i], widths[i],
            false);
      }
      appendStringValue(status, MasterStatus.HEADERS[i], widths[i], true);

      // append separator
      for (i = 0; i < MasterStatus.MASTERSTATUS_COLUMNS - 1; i++) {
        appendSeparator(status, widths[i], false);
      }
      appendSeparator(status, widths[i], true);

      // append subscription data
      for (final MasterStatus masterStatus : masters) {
        appendStringValue(status, masterStatus.getSchema(),
            widths[MASTERSTATUS_SCHEMA], false);
        appendStringValue(status, masterStatus.getTable(),
            widths[MASTERSTATUS_TABLE], false);
        appendLongValue(status, masterStatus.getLogCount(),
            widths[MASTERSTATUS_LOGS], false);
        appendLongValue(status, masterStatus.getSubscribers(),
            widths[MASTERSTATUS_SUBS], false);
        appendTimestampValue(status, masterStatus.getOldestRefresh(),
            false);
        appendStringValue(status, masterStatus.getOldestReplica(),
            widths[MASTERSTATUS_OLDEST_REPLICA], true);
      }

      return status.toString();
    } finally {
      // release resources
      if (conn != null) {
        conn.close();
      }
      if (db != null) {
        db.stop();
      }
    }
  }
View Full Code Here

      SQLException {
    if (server == null) {
      throw new SyncDatabaseException("error.argument");
    }

    DatabaseResource db = null;
    Connection conn = null;

    try {
      // get replica server connection
      db = new DatabaseResource(server);
      conn = db.getConnection();

      final ArrayList<ReplicaStatus> replicas = SyncDatabaseDAO
          .getReplicaStatus(conn, schema, table);
      final StringBuilder status = new StringBuilder();
      status.append("replica status\n");

      // get display size
      final int[] widths = new int[REPLICASTATUS_COLUMNS];
      for (int i = 0; i < REPLICASTATUS_COLUMNS; i++) {
        widths[i] = ReplicaStatus.HEADERS[i].length();
      }

      widths[REPLICASTATUS_LAST_REFRESH] = TIMESTAMP_WIDTH;
      for (final ReplicaStatus replicaStatus : replicas) {
        if (replicaStatus.getSchema() != null) {
          widths[REPLICASTATUS_SCHEMA] = Math.max(
              widths[REPLICASTATUS_SCHEMA], replicaStatus
                  .getSchema().getBytes().length);
        }

        if (replicaStatus.getTable() != null) {
          widths[REPLICASTATUS_TABLE] = Math.max(
              widths[REPLICASTATUS_TABLE], replicaStatus
                  .getTable().getBytes().length);
        }

        widths[REPLICASTATUS_MASTER] = Math.max(
            widths[REPLICASTATUS_MASTER], replicaStatus.getMaster()
                .getBytes().length);
      }

      // append header
      int i;
      for (i = 0; i < REPLICASTATUS_MASTER; i++) {
        appendStringValue(status, ReplicaStatus.HEADERS[i], widths[i],
            false);
      }
      appendStringValue(status,
          ReplicaStatus.HEADERS[REPLICASTATUS_MASTER],
          widths[REPLICASTATUS_MASTER], !cost);
      if (cost) {
        appendStringValue(status,
            ReplicaStatus.HEADERS[REPLICASTATUS_COST],
            widths[REPLICASTATUS_COST], true);
      }

      // append separator
      for (i = 0; i < REPLICASTATUS_MASTER; i++) {
        appendSeparator(status, widths[i], false);
      }
      appendSeparator(status, widths[REPLICASTATUS_MASTER], !cost);
      if (cost) {
        appendSeparator(status, widths[REPLICASTATUS_COST], true);
      }

      // append subscription data
      for (final ReplicaStatus replicaStatus : replicas) {
        appendStringValue(status, replicaStatus.getSchema(),
            widths[REPLICASTATUS_SCHEMA], false);
        appendStringValue(status, replicaStatus.getTable(),
            widths[REPLICASTATUS_TABLE], false);
        appendTimestampValue(status, replicaStatus.getLastRefresh(),
            false);
        appendStringValue(status, replicaStatus.getMaster(),
            widths[REPLICASTATUS_MASTER], !cost);
        if (cost) {
          double incrementalCost = Double.NaN;
          try {
            incrementalCost = getCost(replicaStatus.getMaster(),
                replicaStatus.getSubsID());
          } catch (Exception e) {
            log.debug(e.getMessage());
            log.warn(mProperty
                .getMessage("warning.master.broken",
                    SyncDatabaseDAO.getTablePrint(
                        replicaStatus.getSchema(),
                        replicaStatus.getTable())));
          }
          appendDoubleValue(status, incrementalCost,
              widths[REPLICASTATUS_COST], true);
        }
      }

      return status.toString();
    } finally {
      // release resources
      if (conn != null) {
        conn.close();
      }
      if (db != null) {
        db.stop();
      }
    }
  }
View Full Code Here

      SQLException {
    if (master == null || subsid == Subscription.NOT_HAVE_SUBSCRIBER) {
      return Double.NaN;
    }

    DatabaseResource db = null;
    Connection conn = null;
    double cost;

    try {
      // get master server connection
      db = new DatabaseResource(master);
      conn = db.getConnection();

      Subscriber suber = SyncDatabaseDAO.getSubscriber(conn, subsid);

      if (suber.getNspName() == null || suber.getRelName() == null) {
        throw new SyncDatabaseException("error.master.dropped", subsid);
      }

      cost = SyncDatabaseDAO.getIncrementalRefreshCost(conn, suber
          .getMlogName(), suber.getLastMlogID(),
          suber.getLastCount(), SyncDatabaseDAO.getPKNames(conn,
              suber.getNspName(), suber.getRelName()));
    } finally {
      // release resources
      if (conn != null) {
        conn.close();
      }
      if (db != null) {
        db.stop();
      }
    }

    return cost;
  }
View Full Code Here

  protected static String newLine;

  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    // need connection test
    masterDB = new DatabaseResource("postgres1");
    replicaDB = new DatabaseResource("postgres2");
    masterConn = masterDB.getConnection();
    replicaConn = replicaDB.getConnection();
    oraDB = new DatabaseResource("oracle");
    oraConn = oraDB.getConnection();

    // private method test
    String[] args = { "refresh", "--server", "postgres2", "--schema",
        "public", "--table", "rep_tab1", "--mode", "full" };
View Full Code Here

    if (showHelp) {
      showHelp();
      return;
    }

    DatabaseResource replicaDB = null;
    DatabaseResource masterDB = null;
    Connection replicaConn = null;
    Connection masterConn = null;
    UserTransaction utx = null;
    long subsid = 0;

    try {
      // get replica server connection
      replicaDB = new DatabaseResource(server);
      replicaConn = replicaDB.getConnection();

      // get master server connection
      masterDB = new DatabaseResource(master);
      masterConn = masterDB.getConnection();

      // parse query
      final DatabaseMetaData dmd = masterConn.getMetaData();
      final String quoteString = dmd.getIdentifierQuoteString();
      final QueryParser parser = new QueryParser(query, quoteString);
      if (mode == RefreshMode.FULL) {
        parser.parseFull();
      } else {
        parser.parseIncremental();
      }

      // begin transaction
      utx = replicaDB.getUserTransaction();
      utx.begin();

      if (mode == RefreshMode.INCREMENTAL) {
        subsid = SyncDatabaseDAO.subscribeMlog(masterConn, parser
            .getSchema(), parser.getTable(), SyncDatabaseDAO
            .getDescription(replicaConn, server));
      }

      SyncDatabaseDAO.subscribeObserver(replicaConn, schema, table,
          subsid, master, query);

      // commit transaction
      utx.commit();
    } catch (final Exception e) {
      // rollback transaction
      if (utx != null && utx.getStatus() != Status.STATUS_NO_TRANSACTION) {
        utx.rollback();
      }

      throw e;
    } finally {
      // release resources
      if (replicaConn != null) {
        replicaConn.close();
      }
      if (replicaDB != null) {
        replicaDB.stop();
      }
      if (masterConn != null) {
        masterConn.close();
      }
      if (masterDB != null) {
        masterDB.stop();
      }
    }

    if (mode == RefreshMode.FULL) {
      log.info(mProperty.getMessage("info.attach.full"));
View Full Code Here

    if (showHelp) {
      showHelp();
      return;
    }

    DatabaseResource replicaDB = null;
    DatabaseResource masterDB = null;
    Connection replicaConn = null;
    Connection masterConn = null;
    UserTransaction utx = null;
    boolean updated = false;
    boolean detached = false;

    try {
      Subscription subs = null;
      Subscriber suber = null;

      // get replica server connection
      replicaDB = new DatabaseResource(server);
      replicaConn = replicaDB.getConnection();

      // begin transaction
      utx = replicaDB.getUserTransaction();
      utx.begin();

      // get subscription data
      subs = SyncDatabaseDAO.getSubscription(replicaConn, schema, table);

      if (subs.getSubsID() != Subscription.NOT_HAVE_SUBSCRIBER) {
        // get master server connection
        try {
          masterDB = new DatabaseResource(subs.getSrvname());
          masterConn = masterDB.getConnection();
        } catch (Exception e) {
          if (!force) {
            throw e;
          }

          log.warn(e.getMessage());
        }
      }

      // unsubscribe replica
      SyncDatabaseDAO.unSubscribeObserver(replicaConn, schema, table);

      if (masterConn != null) {
        if (force) {
          utx.commit();
          updated = true;
          utx.begin();
        }

        // get subscriber data
        suber = SyncDatabaseDAO.getSubscriber(masterConn, subs
            .getSubsID());

        if (suber.getNspName() == null || suber.getRelName() == null) {
          log.warn(mProperty.getMessage("error.master.dropped", suber
              .getSubsID()));
        }

        // unsubscribe master
        SyncDatabaseDAO.unSubscribeMlog(masterConn, suber.getSubsID());
      }

      // commit transaction
      utx.commit();
      log.info(mProperty.getMessage("info.detach.success"));
      detached = true;

      // purge
      if (suber != null) {
        if (suber.getNspName() == null || suber.getRelName() == null) {
          log.debug("no purge");
          return;
        }

        utx.setTransactionTimeout(DatabaseResource.DEFAULT_TIMEOUT);
        utx.begin();
        SyncDatabaseDAO.purgeMlog(masterConn, suber.getNspName(), suber
            .getRelName());

        // commit transaction
        utx.commit();
      }
    } catch (final Exception e) {
      // rollback transaction
      if (utx != null && utx.getStatus() != Status.STATUS_NO_TRANSACTION) {
        utx.rollback();
      }

      if (!(detached || (force && updated))) {
        throw e;
      }

      log.warn(e.getMessage());
    } finally {
      // release resources
      if (replicaConn != null) {
        replicaConn.close();
      }
      if (replicaDB != null) {
        replicaDB.stop();
      }
      if (masterConn != null) {
        masterConn.close();
      }
      if (masterDB != null) {
        masterDB.stop();
      }
    }
  }
View Full Code Here

  protected PrintStream _out;
  protected static String newLine;

  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    replicaDB = new DatabaseResource("postgres2");
    replicaConn = replicaDB.getConnection();
  }
View Full Code Here

  private static DatabaseResource masterDB = null;
  private static Connection masterConn = null;

  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    masterDB = new DatabaseResource("postgres1");
    masterConn = masterDB.getConnection();
  }
View Full Code Here

  private static DatabaseResource replicaDB = null;
  private static Connection replicaConn = null;

  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    replicaDB = new DatabaseResource("postgres2");
    replicaConn = replicaDB.getConnection();
  }
View Full Code Here

TOP

Related Classes of jp.co.ntt.oss.data.DatabaseResource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.