Search in sources :

Example 6 with CacheExistsException

use of org.apache.geode.cache.CacheExistsException in project geode by apache.

the class LRUClockJUnitTest method getARegion.

private Region getARegion() throws Exception {
    DistributedSystem ds = DistributedSystem.connect(sysProps);
    Cache c = null;
    try {
        c = CacheFactory.create(ds);
    } catch (CacheExistsException cee) {
        c = CacheFactory.getInstance(ds);
    }
    AttributesFactory af = new AttributesFactory();
    Region root = c.getRegion("root");
    if (root == null) {
        root = c.createRegion("root", af.create());
    }
    Region sub = root.createSubregion(testName.getMethodName(), af.create());
    return sub;
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheExistsException(org.apache.geode.cache.CacheExistsException) Region(org.apache.geode.cache.Region) PlaceHolderDiskRegion(org.apache.geode.internal.cache.PlaceHolderDiskRegion) DistributedSystem(org.apache.geode.distributed.DistributedSystem) Cache(org.apache.geode.cache.Cache)

Example 7 with CacheExistsException

use of org.apache.geode.cache.CacheExistsException in project geode by apache.

the class JUnit4CacheTestCase method createLonerCache.

/**
   * Creates the {@code Cache} for this test that is not connected to other members.
   */
public final Cache createLonerCache() {
    synchronized (JUnit4CacheTestCase.class) {
        try {
            System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "DISABLE_DISCONNECT_DS_ON_CACHE_CLOSE", "true");
            InternalCache newCache = (InternalCache) CacheFactory.create(getLonerSystem());
            cache = newCache;
        } catch (CacheExistsException e) {
            // TODO: remove error handling
            Assert.fail("the cache already exists", e);
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            Assert.fail("Checked exception while initializing cache??", ex);
        } finally {
            System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + "DISABLE_DISCONNECT_DS_ON_CACHE_CLOSE");
        }
        return cache;
    }
}
Also used : CacheExistsException(org.apache.geode.cache.CacheExistsException) InternalCache(org.apache.geode.internal.cache.InternalCache) RegionExistsException(org.apache.geode.cache.RegionExistsException) TimeoutException(org.apache.geode.cache.TimeoutException) CacheExistsException(org.apache.geode.cache.CacheExistsException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException)

Example 8 with CacheExistsException

use of org.apache.geode.cache.CacheExistsException in project geode by apache.

the class JUnit4CacheTestCase method createCache.

private final void createCache(final boolean client, final CacheFactory factory) {
    synchronized (JUnit4CacheTestCase.class) {
        try {
            System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "DISABLE_DISCONNECT_DS_ON_CACHE_CLOSE", "true");
            InternalCache newCache;
            if (client) {
                System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "locators", "");
                System.setProperty(DistributionConfig.GEMFIRE_PREFIX + MCAST_PORT, "0");
                newCache = (InternalCache) new ClientCacheFactory(getSystem().getProperties()).create();
            } else {
                if (factory == null) {
                    newCache = (InternalCache) CacheFactory.create(getSystem());
                } else {
                    Properties props = getSystem().getProperties();
                    for (Map.Entry entry : props.entrySet()) {
                        factory.set((String) entry.getKey(), (String) entry.getValue());
                    }
                    newCache = (InternalCache) factory.create();
                }
            }
            cache = newCache;
        } catch (CacheExistsException e) {
            // TODO: remove error handling
            Assert.fail("the cache already exists", e);
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            Assert.fail("Checked exception while initializing cache??", ex);
        } finally {
            System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + "DISABLE_DISCONNECT_DS_ON_CACHE_CLOSE");
            System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + "locators");
            System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + MCAST_PORT);
        }
    }
}
Also used : CacheExistsException(org.apache.geode.cache.CacheExistsException) InternalCache(org.apache.geode.internal.cache.InternalCache) Properties(java.util.Properties) Map(java.util.Map) RegionExistsException(org.apache.geode.cache.RegionExistsException) TimeoutException(org.apache.geode.cache.TimeoutException) CacheExistsException(org.apache.geode.cache.CacheExistsException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory)

Example 9 with CacheExistsException

use of org.apache.geode.cache.CacheExistsException in project geode by apache.

the class CacheJUnitTest method testScenario5.

/**
   * Test of testScenario5 method, of class org.apache.geode.internal.jta.functional.CacheTest1.
   * Tests whether a user transaction with cache lookup and with XAPooledDataSOurce supports Cache
   * put and get operations accordingly along with JTA behavior. Put and get are done within the
   * transaction block and also db updates are done. We try to rollback the transaction by violating
   * primary key constraint in db table, nad then we check whether rollback was proper in db and
   * also in Cache, which should also rollback.
   */
@Test
public void testScenario5() throws Exception {
    this.tblIDFld = 5;
    this.tblNameFld = "test5";
    boolean rollback_chances = false;
    final String DEFAULT_RGN = "root";
    JTAUtils jtaObj = new JTAUtils(cache, currRegion);
    jtaObj.deleteRows(this.tblName);
    Context ctx = cache.getJNDIContext();
    UserTransaction ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
    Connection conn = null;
    try {
        String current_region = jtaObj.currRegion.getName();
        assertEquals("default region is not root", DEFAULT_RGN, current_region);
        // trying to create a region 'region1' under /root, if it doesn't exist.
        // And point to the new region...
        jtaObj.getRegionFromCache("region1");
        // now current region should point to region1, as done from within
        // getRegionFromCache method...
        String current_fullpath = jtaObj.currRegion.getFullPath();
        assertEquals("failed retirieving current fullpath", "/" + DEFAULT_RGN + "/region1", current_fullpath);
        jtaObj.put("key1", "test");
        ta.begin();
        jtaObj.put("key1", "value1");
        String str = jtaObj.get("key1");
        String tok = jtaObj.parseGetValue(str);
        assertEquals("get value mismatch with put", "\"value1\"", tok);
        current_fullpath = jtaObj.currRegion.getFullPath();
        assertEquals("failed retrieving current fullpath, current fullpath: " + current_fullpath, "/" + DEFAULT_RGN + "/region1", current_fullpath);
        DataSource da = (DataSource) ctx.lookup("java:/XAPooledDataSource");
        conn = da.getConnection();
        Statement stmt = conn.createStatement();
        String sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
        stmt.executeUpdate(sqlSTR);
        rollback_chances = true;
        // capable of throwing SQLException during insert operation
        sqlSTR = "insert into " + this.tblName + " values (" + this.tblIDFld + "," + "'" + this.tblNameFld + "'" + ")";
        stmt.executeUpdate(sqlSTR);
        stmt.close();
        ta.commit();
        conn.close();
    } catch (CacheExistsException e) {
        ta.rollback();
        fail("test failed " + e.getMessage());
    } catch (NamingException e) {
        ta.rollback();
        fail("test failed " + e.getMessage());
    } catch (SQLException e) {
        // exception thrown during inserts are handeled as below by rollback
        if (rollback_chances) {
            try {
                ta.rollback();
            } catch (Exception ex) {
                fail("failed: " + ex.getMessage());
            }
            // intended error is checked w.r.t database first
            int ifAnyRows = 0;
            try {
                ifAnyRows = jtaObj.getRows(this.tblName);
            } catch (Exception ex) {
                fail(" failed: " + ex.getMessage());
            }
            assertEquals("rows found after rollback is: " + ifAnyRows, 0, ifAnyRows);
            /*
         * if (ifAnyRows != 0) { fail (" DB FAILURE"); }
         */
            // intended error is checked w.r.t. cache second
            String current_fullpath = jtaObj.currRegion.getFullPath();
            assertEquals("failed retrieving current fullpath after rollback, fullpath is: " + current_fullpath, "/" + DEFAULT_RGN + "/region1", current_fullpath);
            // after jdbc rollback, cache value in region1 for key1 must vanish...
            String str1 = null;
            try {
                str1 = jtaObj.get("key1");
            } catch (CacheException ex) {
                fail("failed getting value for 'key1': " + ex.getMessage());
            }
            String tok1 = jtaObj.parseGetValue(str1);
            assertEquals("value found in cache: " + tok1 + ", after rollback", "\"test\"", tok1);
        } else {
            fail(" failed: " + e.getMessage());
            ta.rollback();
        }
    } catch (Exception e) {
        ta.rollback();
        fail(" failed: " + e.getMessage());
    } finally {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ex) {
                fail("Exception: " + ex.getMessage());
            }
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) SQLException(java.sql.SQLException) CacheException(org.apache.geode.cache.CacheException) Statement(java.sql.Statement) Connection(java.sql.Connection) JTAUtils(org.apache.geode.internal.jta.JTAUtils) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheExistsException(org.apache.geode.cache.CacheExistsException) CacheException(org.apache.geode.cache.CacheException) DataSource(javax.sql.DataSource) CacheExistsException(org.apache.geode.cache.CacheExistsException) NamingException(javax.naming.NamingException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 10 with CacheExistsException

use of org.apache.geode.cache.CacheExistsException in project geode by apache.

the class ConcurrentIndexUpdateWithInplaceObjectModFalseDUnitTest method getAvailableCacheElseCreateCache.

private void getAvailableCacheElseCreateCache() {
    synchronized (ConcurrentIndexUpdateWithInplaceObjectModFalseDUnitTest.class) {
        try {
            Cache newCache = GemFireCacheImpl.getInstance();
            if (null == newCache) {
                System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "DISABLE_DISCONNECT_DS_ON_CACHE_CLOSE", "true");
                newCache = CacheFactory.create(getSystem());
            }
            PRQueryDUnitHelper.setCache(newCache);
        } catch (CacheExistsException e) {
            // TODO: remove error handling
            Assert.fail("the cache already exists", e);
        } catch (RuntimeException ex) {
            throw ex;
        } catch (Exception ex) {
            Assert.fail("Checked exception while initializing cache??", ex);
        } finally {
            System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + "DISABLE_DISCONNECT_DS_ON_CACHE_CLOSE");
        }
    }
}
Also used : CacheExistsException(org.apache.geode.cache.CacheExistsException) CacheExistsException(org.apache.geode.cache.CacheExistsException) CacheException(org.apache.geode.cache.CacheException) Cache(org.apache.geode.cache.Cache)

Aggregations

CacheExistsException (org.apache.geode.cache.CacheExistsException)11 CacheException (org.apache.geode.cache.CacheException)9 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 Context (javax.naming.Context)4 NamingException (javax.naming.NamingException)4 DataSource (javax.sql.DataSource)4 UserTransaction (javax.transaction.UserTransaction)4 Cache (org.apache.geode.cache.Cache)4 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)4 JTAUtils (org.apache.geode.internal.jta.JTAUtils)4 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 Statement (java.sql.Statement)3 RegionExistsException (org.apache.geode.cache.RegionExistsException)2 TimeoutException (org.apache.geode.cache.TimeoutException)2 InternalCache (org.apache.geode.internal.cache.InternalCache)2 IgnoredException (org.apache.geode.test.dunit.IgnoredException)2 File (java.io.File)1