⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pessimisticisolatedclassloadertest.java

📁 hibernate-distribution-3.3.1.GA-dist.zip源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   
   public void testClassLoaderHandlingStandardQueryCache() throws Exception {
      queryTest(false);
   }
   
   protected void queryTest(boolean useNamedRegion) throws Exception
   {
      // Bind a listener to the "local" cache
      // Our region factory makes its CacheManager available to us
      CacheManager localManager = TestCacheInstanceManager.getTestCacheManager(DualNodeTestUtil.LOCAL);
      this.localCache = localManager.getCache(getEntityCacheConfigName(), true);
      this.localListener = new CacheAccessListener();
      localCache.addCacheListener(localListener);
      
      TransactionManager localTM = localCache.getConfiguration().getRuntimeConfig().getTransactionManager();
      
      // Bind a listener to the "remote" cache
      CacheManager remoteManager = TestCacheInstanceManager.getTestCacheManager(DualNodeTestUtil.REMOTE);
      this.remoteCache = remoteManager.getCache(getEntityCacheConfigName(), true);
      this.remoteListener = new CacheAccessListener();
      remoteCache.addCacheListener(remoteListener);      
      
      TransactionManager remoteTM = remoteCache.getConfiguration().getRuntimeConfig().getTransactionManager();
      
      SessionFactory localFactory = getEnvironment().getSessionFactory();
      SessionFactory remoteFactory = getSecondNodeEnvironment().getSessionFactory();
      
      ClassLoaderTestDAO dao0 = new ClassLoaderTestDAO(localFactory, localTM);      
      ClassLoaderTestDAO dao1 = new ClassLoaderTestDAO(remoteFactory, remoteTM);
      
      // Determine whether our query region is already there (in which case it
      // will receive remote messages immediately) or is yet to be created on
      // first use (in which case it will initially discard remote messages)
      String regionName = createRegionName(useNamedRegion ? "AccountRegion" : StandardQueryCache.class.getName());
      Region queryRegion = remoteCache.getRegion(Fqn.fromString(regionName), false);
      boolean queryRegionExists = queryRegion != null && queryRegion.isActive();
      
      // Initial ops on node 0
      setupEntities(dao0);
      
      // Query on post code count
      assertEquals("63088 has correct # of accounts", 6, dao0.getCountForBranch("63088", useNamedRegion));
      
      assertTrue("Query cache used " + regionName, 
            localListener.getSawRegionModification(regionName));
      // Clear the access state
      localListener.getSawRegionAccess(regionName);
      
      log.info("First query on node0 done");
      
      // Sleep a bit to allow async repl to happen
      sleep(SLEEP_TIME);
      
      // If region isn't activated yet, should not have been modified      
      if (!queryRegionExists)
      {
         assertFalse("Query cache remotely modified " + regionName, 
               remoteListener.getSawRegionModification(regionName));
         // Clear the access state
         remoteListener.getSawRegionAccess(regionName);
      }
      else
      {
         assertTrue("Query cache remotely modified " + regionName, 
               remoteListener.getSawRegionModification(regionName));
         // Clear the access state
         remoteListener.getSawRegionAccess(regionName);         
      }
      
      // Do query again from node 1      
      assertEquals("63088 has correct # of accounts", 6, dao1.getCountForBranch("63088", useNamedRegion));
      
      if (!queryRegionExists)
      {
         // Query should have activated the region and then been inserted
         assertTrue("Query cache modified " + regionName, 
               remoteListener.getSawRegionModification(regionName));
         // Clear the access state
         remoteListener.getSawRegionAccess(regionName);
      }
      
      log.info("First query on node 1 done");
      
      // We now have the query cache region activated on both nodes.
      
      // Sleep a bit to allow async repl to happen
      sleep(SLEEP_TIME);
      
      // Do some more queries on node 0
      
      assertEquals("Correct branch for Smith", "94536", dao0.getBranch(dao0.getSmith(), useNamedRegion));
      
      assertEquals("Correct high balances for Jones", 40, dao0.getTotalBalance(dao0.getJones(), useNamedRegion));
      
      assertTrue("Query cache used " + regionName, 
            localListener.getSawRegionModification(regionName));
      // Clear the access state
      localListener.getSawRegionAccess(regionName);
      
      log.info("Second set of queries on node0 done");
      
      // Sleep a bit to allow async repl to happen
      sleep(SLEEP_TIME);
             
      // Check if the previous queries replicated      
      assertTrue("Query cache remotely modified " + regionName, 
            remoteListener.getSawRegionModification(regionName));
      // Clear the access state
      remoteListener.getSawRegionAccess(regionName);
      
      // Do queries again from node 1      
      assertEquals("Correct branch for Smith", "94536", dao1.getBranch(dao1.getSmith(), useNamedRegion));
      
      assertEquals("Correct high balances for Jones", 40, dao1.getTotalBalance(dao1.getJones(), useNamedRegion));
      
      // Should be no change; query was already there
      assertFalse("Query cache modified " + regionName, 
            remoteListener.getSawRegionModification(regionName));
      assertTrue("Query cache accessed " + regionName, 
            remoteListener.getSawRegionAccess(regionName));
      
      log.info("Second set of queries on node1 done");
      
      // allow async to propagate
      sleep(SLEEP_TIME);
      
      // Modify underlying data on node 1
      modifyEntities(dao1);
      
      // allow async timestamp change to propagate
      sleep(SLEEP_TIME);
      
      // Confirm query results are correct on node 0
      
      assertEquals("63088 has correct # of accounts", 7, dao0.getCountForBranch("63088", useNamedRegion));
      
      assertEquals("Correct branch for Smith", "63088", dao0.getBranch(dao0.getSmith(), useNamedRegion));
      
      assertEquals("Correct high balances for Jones", 50, dao0.getTotalBalance(dao0.getJones(), useNamedRegion));
      
      log.info("Third set of queries on node0 done");
   }
   
   protected String createRegionName(String noPrefix)
   {
      String combined = getRegionPrefix() == null ? noPrefix : getRegionPrefix() + '.' + noPrefix;
      return BasicRegionAdapter.getTypeLastRegionFqn(combined, getRegionPrefix(), QueryResultsRegionImpl.TYPE).toString();
   }
   
   protected void setupEntities(ClassLoaderTestDAO dao) throws Exception
   {
      dao.cleanup();
      
      dao.createAccount(dao.getSmith(), new Integer(1001), new Integer(5), "94536");
      dao.createAccount(dao.getSmith(), new Integer(1002), new Integer(15), "94536");
      dao.createAccount(dao.getSmith(), new Integer(1003), new Integer(20), "94536");
      
      dao.createAccount(dao.getJones(), new Integer(2001), new Integer(5), "63088");
      dao.createAccount(dao.getJones(), new Integer(2002), new Integer(15), "63088");
      dao.createAccount(dao.getJones(), new Integer(2003), new Integer(20), "63088");
      
      dao.createAccount(dao.getBarney(), new Integer(3001), new Integer(5), "63088");
      dao.createAccount(dao.getBarney(), new Integer(3002), new Integer(15), "63088");
      dao.createAccount(dao.getBarney(), new Integer(3003), new Integer(20), "63088");
      
      log.info("Standard entities created");
   }
   
   protected void resetRegionUsageState(CacheAccessListener localListener, CacheAccessListener remoteListener)
   {  
      String stdName = createRegionName(StandardQueryCache.class.getName());
      String acctName = createRegionName("AccountRegion");
      
      localListener.getSawRegionModification(stdName);
      localListener.getSawRegionModification(acctName);
      
      localListener.getSawRegionAccess(stdName);
      localListener.getSawRegionAccess(acctName);
      
      remoteListener.getSawRegionModification(stdName);
      remoteListener.getSawRegionModification(acctName);
      
      remoteListener.getSawRegionAccess(stdName);
      remoteListener.getSawRegionAccess(acctName);
      
      log.info("Region usage state cleared");      
   }
   
   protected void modifyEntities(ClassLoaderTestDAO dao) throws Exception
   {
      dao.updateAccountBranch(1001, "63088");
      dao.updateAccountBalance(2001, 15);
      
      log.info("Entities modified");
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -