Package jp.co.ntt.oss.data

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


    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();
      }
View Full Code Here


    // normal case
    PrivateAccessor.setPrivateField(com, "table", "rep_foo_inc");
    PrivateAccessor.setPrivateField(com, "mode", RefreshMode.INCREMENTAL);
    ResultSet rset = null;
    try {
      Subscriber suber = SyncDatabaseDAO.getSubscriber(masterConn, 8);
      assertEquals(7, suber.getLastCount());
      assertEquals("F", suber.getLastType());
      assertEquals(1, suber.getLastMlogID());
      String suberTime = suber.getLastTime().toString();

      Subscription subs = SyncDatabaseDAO.getSubscription(replicaConn,
          "public", "rep_foo_inc");
      assertEquals("F", subs.getLastType());
      String subsTime = subs.getLastTime().toString();

      Statement stmt = replicaConn.createStatement();

      com.execute();

      rset = stmt
          .executeQuery("SELECT * FROM public.rep_foo_inc ORDER BY val1");

      assertTrue(rset.next());
      assertEquals(1, rset.getInt(1));
      assertEquals("A", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:01"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(2, rset.getInt(1));
      assertEquals("B", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:02"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(3, rset.getInt(1));
      assertEquals("C", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:03"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(4, rset.getInt(1));
      assertEquals("D", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:04"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(5, rset.getInt(1));
      assertEquals("E", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:05"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(6, rset.getInt(1));
      assertEquals("F", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:06"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(7, rset.getInt(1));
      assertEquals("G", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:07"), rset
          .getTimestamp(3));

      assertFalse(rset.next());

      suber = SyncDatabaseDAO.getSubscriber(masterConn, 8);
      assertEquals(7, suber.getLastCount());
      assertEquals("I", suber.getLastType());
      assertEquals(28, suber.getLastMlogID());
      assertFalse(suber.getLastTime().toString().equals(suberTime));

      subs = SyncDatabaseDAO.getSubscription(replicaConn, "public",
          "rep_foo_inc");
      assertEquals("I", subs.getLastType());
      assertFalse(subs.getLastTime().toString().equals(subsTime));
View Full Code Here

    Subscription subs = new Subscription("public", "rep_foo",
        "public.rep_foo", "attachuser", 1, "srvname",
        "SELECT * FROM public.foo", Timestamp
            .valueOf("2010-01-01 12:34:56"), "F");

    Subscriber suber = null;
    try {
      suber = SyncDatabaseDAO.getSubscriber(masterConn, 5);
    } catch (Exception e) {
      fail("exception thrown");
    }

    Method method = PrivateAccessor.getPrivateMethod(com, "fullRefresh");

    // null argument
    try {
      method.invoke(com, null, replicaConn, suber, subs, true);
      fail("no exception");
    } catch (InvocationTargetException e) {
      actual = e.getTargetException().getMessage();
      assertEquals("argument error", actual);
    } catch (Exception e) {
      fail("exception thrown");
    }
    try {
      method.invoke(com, masterConn, null, suber, subs, true);
      fail("no exception");
    } catch (InvocationTargetException e) {
      actual = e.getTargetException().getMessage();
      assertEquals("argument error", actual);
    } catch (Exception e) {
      fail("exception thrown");
    }
    try {
      method.invoke(com, masterConn, replicaConn, suber, null, true);
      fail("no exception");
    } catch (InvocationTargetException e) {
      actual = e.getTargetException().getMessage();
      assertEquals("argument error", actual);
    } catch (Exception e) {
      fail("exception thrown");
    }
    Statement stmt = null;
    ResultSet rset = null;
    try {
      stmt = replicaConn.createStatement();
      rset = stmt.executeQuery("SELECT relfilenode FROM "
          + "pg_catalog.pg_class WHERE relname = 'rep_foo'");
      assertTrue(rset.next());
      String beforeFilenode = rset.getString(1);

      String result = RefreshCommand.fullRefresh(masterConn, replicaConn,
          suber, subs, false);
      assertEquals("full refresh (insert:7)", result);

      rset.close();
      rset = stmt.executeQuery("SELECT relfilenode FROM "
          + "pg_catalog.pg_class WHERE relname = 'rep_foo'");
      assertTrue(rset.next());
      String afterFilenode = rset.getString(1);
      assertFalse(beforeFilenode.equalsIgnoreCase(afterFilenode));

      rset.close();
      rset = stmt.executeQuery("SELECT * FROM public.rep_foo");

      assertTrue(rset.next());
      assertEquals(1, rset.getInt(1));
      assertEquals("A", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:01"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(2, rset.getInt(1));
      assertEquals("B", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:02"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(3, rset.getInt(1));
      assertEquals("C", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:03"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(4, rset.getInt(1));
      assertEquals("D", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:04"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(5, rset.getInt(1));
      assertEquals("E", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:05"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(6, rset.getInt(1));
      assertEquals("F", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:06"), rset
          .getTimestamp(3));

      assertTrue(rset.next());
      assertEquals(7, rset.getInt(1));
      assertEquals("G", rset.getString(2));
      assertEquals(Timestamp.valueOf("2010-01-01 00:00:07"), rset
          .getTimestamp(3));

      assertFalse(rset.next());

      // Subscriber
      assertEquals(5, suber.getSubsID());
      assertEquals("public.foo", suber.getMasterTableName());
      assertEquals("mlog.mlog$", suber.getMlogName().substring(0, 10));
      assertEquals(
          "resource name:\"postgres2\", DBMS:\"PostgreSQL\", URL:\"jdbc:postgresql://\"",
          suber.getDescription());
      assertEquals(Timestamp.valueOf("2010-01-01 12:34:56"), suber
          .getLastTime());
      assertEquals("F", suber.getLastType());
      assertEquals(0, suber.getLastMlogID());
      assertEquals(7, suber.getLastCount());

      // Subscription
      assertEquals("public", subs.getSchema());
      assertEquals("rep_foo", subs.getTable());
      assertEquals(1, subs.getSubsID());
View Full Code Here

  }

  @Test
  public final void testIncrementalRefresh() {
    Subscription subs = null;
    Subscriber suber = null;
    try {
      subs = SyncDatabaseDAO.getSubscription(replicaConn, "public",
          "rep_foo_inc");
      suber = SyncDatabaseDAO.getSubscriber(masterConn, 8);
    } catch (Exception e1) {
      fail("exception thrown");
    }

    // argument error
    try {
      RefreshCommand.incrementalRefresh(null, replicaConn, suber, subs);
      fail("no exception");
    } catch (SyncDatabaseException e) {
      assertEquals("argument error", e.getMessage());
    } catch (Exception e) {
      fail("other exception thrown");
    }
    try {
      RefreshCommand.incrementalRefresh(masterConn, null, suber, subs);
      fail("no exception");
    } catch (SyncDatabaseException e) {
      assertEquals("argument error", e.getMessage());
    } catch (Exception e) {
      fail("other exception thrown");
    }
    try {
      RefreshCommand.incrementalRefresh(masterConn, replicaConn, null,
          subs);
      fail("no exception");
    } catch (SyncDatabaseException e) {
      assertEquals("argument error", e.getMessage());
    } catch (Exception e) {
      fail("other exception thrown");
    }
    try {
      RefreshCommand.incrementalRefresh(masterConn, replicaConn, suber,
          null);
      fail("no exception");
    } catch (SyncDatabaseException e) {
      assertEquals("argument error", e.getMessage());
    } catch (Exception e) {
      fail("other exception thrown");
    }

    // normal case
    try {
      subs.setLastType("a");
      suber.setLastType("a");
      suber.setLastCount(100);
      suber.setLastMlogID(100);

      String result = RefreshCommand.incrementalRefresh(masterConn,
          replicaConn, suber, subs);

      assertEquals("incremental refresh (insert:0 update:0 delete:0)",
          result);
      assertEquals("I", subs.getLastType());
      assertEquals("I", suber.getLastType());
      assertEquals(100, suber.getLastCount());

      subs.setLastType("a");
      suber.setLastType("a");
      suber.setLastCount(100);
      suber.setLastMlogID(0);

      result = RefreshCommand.incrementalRefresh(masterConn, replicaConn,
          suber, subs);

      assertEquals("incremental refresh (insert:6 update:1 delete:6)",
          result);
      assertEquals("I", subs.getLastType());
      assertEquals("I", suber.getLastType());
      assertEquals(100, suber.getLastCount());
    } catch (Exception e) {
      e.printStackTrace();
      fail("other exception thrown");
    }
  }
View Full Code Here

    RefreshMode mode;
    Subscription subs = new Subscription("public", "rep_tab1",
        "replicaTable", "attachuser", 1, "srvname",
        "SELECT * FROM public.foo", Timestamp
            .valueOf("2010-01-01 12:34:56"), "F");
    Subscriber suber = null;
    try {
      suber = SyncDatabaseDAO.getSubscriber(masterConn, 6);
      suber.setLastType("F");
    } catch (Exception e2) {
      fail("exception thrown");
    }

    // static value
    assertEquals(-1, Subscriber.NO_REFRESH);

    String actual;
    Method method = PrivateAccessor.getPrivateMethod(com, "getExecuteMode");

    // null argument
    try {
      method.invoke(com, masterConn, suber, null, RefreshMode.FULL);
      fail("no exception");
    } catch (InvocationTargetException e) {
      actual = e.getTargetException().getMessage();
      assertEquals("argument error", actual);
    } catch (Exception e) {
      fail("exception thrown");
    }
    try {
      method.invoke(com, null, suber, subs, RefreshMode.FULL);
      fail("no exception");
    } catch (InvocationTargetException e) {
      actual = e.getTargetException().getMessage();
      assertEquals("argument error", actual);
    } catch (Exception e) {
      fail("exception thrown");
    }

    // FULL refresh
    try {
      mode = (RefreshMode) method.invoke(com, masterConn, suber, subs,
          RefreshMode.FULL);
      assertEquals(RefreshMode.FULL, mode);
    } catch (Exception e) {
      fail("exception thrown");
    }

    // not have subscriber
    try {
      // FULL refresh
      subs.setSubsID(Subscription.NOT_HAVE_SUBSCRIBER);
      mode = (RefreshMode) method.invoke(com, masterConn, suber, subs,
          RefreshMode.AUTO);
      assertEquals(RefreshMode.FULL, mode);
    } catch (Exception e) {
      fail("exception thrown");
    }
    try {
      // FULL refresh only
      mode = (RefreshMode) method.invoke(com, masterConn, suber, subs,
          RefreshMode.INCREMENTAL);
      fail("no exception");
    } catch (InvocationTargetException e) {
      actual = e.getTargetException().getMessage();
      assertEquals("full refresh only", actual);
    } catch (Exception e) {
      fail("exception thrown");
    }
    subs.setSubsID(123);

    // no subscriber
    try {
      mode = (RefreshMode) method.invoke(com, masterConn, null, subs,
          RefreshMode.AUTO);
      assertEquals(RefreshMode.FULL, mode);
      fail("no exception");
    } catch (InvocationTargetException e) {
      actual = e.getTargetException().getMessage();
      assertEquals("not found subscriber id 123", actual);
    } catch (Exception e) {
      fail("exception thrown");
    }

    // not yet first refresh
    try {
      // FULL refresh
      suber.setLastMlogID(Subscriber.NO_REFRESH);
      mode = (RefreshMode) method.invoke(com, masterConn, suber, subs,
          RefreshMode.AUTO);
      assertEquals(RefreshMode.FULL, mode);
    } catch (Exception e) {
      fail("exception thrown");
    }
    try {
      // FULL refresh only
      mode = (RefreshMode) method.invoke(com, masterConn, suber, subs,
          RefreshMode.INCREMENTAL);
      fail("no exception");
    } catch (InvocationTargetException e) {
      actual = e.getTargetException().getMessage();
      assertEquals("full refresh only", actual);
    } catch (Exception e) {
      fail("exception thrown");
    }
    suber.setLastMlogID(456);

    // INCREMENTAL refresh
    try {
      mode = (RefreshMode) method.invoke(com, masterConn, suber, subs,
          RefreshMode.INCREMENTAL);
      assertEquals(RefreshMode.INCREMENTAL, mode);
    } catch (Exception e) {
      fail("exception thrown");
    }

    // choose Fastest Mode -> FULL refresh
    try {
      suber.setLastCount(0);
      suber.setLastMlogID(1);
      mode = (RefreshMode) method.invoke(com, masterConn, suber, subs,
          RefreshMode.AUTO);
      assertEquals(RefreshMode.FULL, mode);
    } catch (Exception e) {
      fail("exception thrown");
    }

    // choose Fastest Mode -> INCREMENTAL refresh
    try {
      suber.setLastCount(1000000);
      mode = (RefreshMode) method.invoke(com, masterConn, suber, subs,
          RefreshMode.AUTO);
      assertEquals(RefreshMode.INCREMENTAL, mode);
    } catch (Exception e) {
      fail("exception thrown");
View Full Code Here

    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();
      }
View Full Code Here

    String result;
    boolean refreshed = false;

    try {
      Subscription subs = null;
      Subscriber suber = null;
      long maxMlogID = SyncDatabaseDAO.MLOG_RECORD_NOT_FOUND;

      // 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.getSchema() == null || subs.getTable() == null) {
        throw new SyncDatabaseException("error.replica.dropped",
            SyncDatabaseDAO.getTablePrint(schema, table));
      }

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

      if (subs.getSubsID() != Subscription.NOT_HAVE_SUBSCRIBER) {
        Connection masterLockConn = null;

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

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

        try {
          masterLockConn = SyncDatabaseDAO.lockTable(subs
              .getSrvname(), suber.getMasterTableName());

          // reset transaction
          utx.rollback();
          utx.setTransactionTimeout(DatabaseResource.DEFAULT_TIMEOUT);
          utx.begin();

          masterConn
              .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

          // first query
          maxMlogID = SyncDatabaseDAO.getMaxMlogID(masterConn, suber
              .getMlogName());

          masterLockConn.rollback();
        } finally {
          // close connection
          if (masterLockConn != null) {
            masterLockConn.close();
          }
        }

        if (maxMlogID == SyncDatabaseDAO.MLOG_RECORD_NOT_FOUND) {
          if (suber.getLastMlogID() == Subscriber.NO_REFRESH) {
            maxMlogID = 0;
          } else {
            maxMlogID = suber.getLastMlogID();
          }
        }

        if (maxMlogID < suber.getLastMlogID()) {
          log.debug("max mlog ID:" + maxMlogID + " / last mlog ID:"
              + suber.getLastMlogID());
          throw new SyncDatabaseException("error.mlog_illegal");
        }
      }

      // get execute refresh mode
      executeMode = getExecuteMode(masterConn, suber, subs, mode);

      if (executeMode == RefreshMode.FULL) {
        // full-refresh
        result = fullRefresh(masterConn, replicaConn, suber, subs,
            concurrent);
      } else {
        // incremental-refresh
        result = incrementalRefresh(masterConn, replicaConn, suber,
            subs);
      }

      /* update subscription */
      SyncDatabaseDAO.setSubscription(replicaConn, subs);

      /* update subscriber */
      if (suber != null) {
        suber.setLastMlogID(maxMlogID);
        SyncDatabaseDAO.setSubscriber(masterConn, suber);
      }

      // commit transaction
      utx.commit();
      log.info(result);
      refreshed = true;

      // purge
      if (suber != null) {
        utx.setTransactionTimeout(DatabaseResource.DEFAULT_TIMEOUT);
        utx.begin();
        SyncDatabaseDAO.purgeMlog(masterConn, suber.getNspName(), suber
            .getRelName());

        // commit transaction
        utx.commit();
      }
View Full Code Here

    }

    // normal case
    PrivateAccessor.setPrivateField(com, "schema", "public");
    PrivateAccessor.setPrivateField(com, "table", "drop1");
    Subscriber suber = null;
    try {
      suber = SyncDatabaseDAO.getSubscriber(masterConn, 14);

      com.execute();

      ArrayList<MasterStatus> status = SyncDatabaseDAO.getMasterStatus(
          masterConn, "public", "drop1");
      assertEquals(0, status.size());
    } catch (Exception e) {
      e.printStackTrace();
      fail("exception thrown");
    }

    // recovery test data
    Statement stmt = null;
    try {
      stmt = masterConn.createStatement();
      stmt.executeUpdate("INSERT INTO mlog.master ("
          + "SELECT 'public.drop1', oid "
          + "FROM (SELECT oid FROM pg_authid "
          + "WHERE rolname = 'syncdbuser') AS a)");
      stmt.executeUpdate("INSERT INTO mlog.subscriber ("
          + "SELECT 14, 'public.drop1', oid, 'desc drop1', "
          + "'2010-01-01 12:34:56'::timestamptz, 'F', 1, 10 FROM "
          + "(SELECT oid FROM pg_authid "
          + "WHERE rolname = 'syncdbuser') AS a)");
      stmt.executeUpdate("CREATE TABLE " + suber.getMlogName()
          + " (mlogid bigint PRIMARY KEY, "
          + "dmltype \"char\", val1 integer)");
      stmt.executeUpdate("CREATE FUNCTION  " + suber.getMlogName()
          + "_trg_fnc() RETURNS void AS '' LANGUAGE sql");
      stmt.close();
    } catch (SQLException e) {
      e.printStackTrace();
      fail("exception thrown");
View Full Code Here

      assertEquals("SELECT * FROM \"public\".\"attach1\"", subs
          .getQuery());
      // assertNull(subs.getLastTime());
      assertNull(subs.getLastType());

      Subscriber suber = SyncDatabaseDAO.getSubscriber(masterConn, subs
          .getSubsID());
      assertEquals(102, suber.getSubsID());
      assertEquals("public", suber.getNspName());
      assertEquals("attach1", suber.getRelName());
      assertEquals("public.attach1", suber.getMasterTableName());
      assertEquals("mlog.mlog$", suber.getMlogName().substring(0, 10));
      assertEquals("createuser", suber.getCreateUser());
      assertEquals("syncdbuser", suber.getAttachUser());
      assertTrue(suber
          .getDescription()
          .matches(
              "resource name:\"postgres2\", DBMS:\"PostgreSQL\", URL:\"jdbc:postgresql://.+"));
      assertNull(suber.getLastTime());
      assertNull(suber.getLastType());
      assertEquals(-1, suber.getLastMlogID());
      assertEquals(-1, suber.getLastCount());
    } catch (Exception e) {
      e.printStackTrace();
      fail("exception thrown");
    }

View Full Code Here

TOP

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

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.