- FeatureLockTest.java
- 
> 
> http://svn.osgeo.org/geotools/trunk/modules/library/main/src/test/java/org/geotools/data/memory/MemoryDataStoreTest.java

Examples
    public void testLockFeatures() throws IOException {
        FeatureLock lock = FeatureLockFactory.generate("test", 3600);
        FeatureLocking<SimpleFeatureType, SimpleFeature> road = 
(FeatureLocking<SimpleFeatureType, SimpleFeature>) 
data.getFeatureSource("road");
        road.setFeatureLock(lock);

        assertFalse(isLocked("road", "road.rd1"));
        road.lockFeatures();
        assertTrue(isLocked("road", "road.rd1"));
    }

public void testUnLockFeatures() throws IOException {
        FeatureLock lock = FeatureLockFactory.generate("test", 3600);
        FeatureLocking<SimpleFeatureType, SimpleFeature> road = 
(FeatureLocking<SimpleFeatureType, SimpleFeature>) 
data.getFeatureSource("road");
        road.setFeatureLock(lock);
        road.lockFeatures();

        try {
            road.unLockFeatures();
            fail("unlock should fail due on AUTO_COMMIT");
        } catch (IOException expected) {
        }
        Transaction t = new DefaultTransaction();
        road.setTransaction(t);
        try {
            road.unLockFeatures();
            fail("unlock should fail due lack of authorization");
        } catch (IOException expected) {
        }
        t.addAuthorization(lock.getAuthorization());
        road.unLockFeatures();
    }

public void testLockFeatureInteraction() throws IOException {
        FeatureLock lockA = FeatureLockFactory.generate("LockA", 3600);
        FeatureLock lockB = FeatureLockFactory.generate("LockB", 3600);
        Transaction t1 = new DefaultTransaction();
        Transaction t2 = new DefaultTransaction();
        FeatureLocking<SimpleFeatureType, SimpleFeature> road1 = 
(FeatureLocking<SimpleFeatureType, SimpleFeature>) 
data.getFeatureSource("road");
        FeatureLocking<SimpleFeatureType, SimpleFeature> road2 = 
(FeatureLocking<SimpleFeatureType, SimpleFeature>) 
data.getFeatureSource("road");
        road1.setTransaction(t1);
        road2.setTransaction(t2);
        road1.setFeatureLock(lockA);
        road2.setFeatureLock(lockB);

        assertFalse(isLocked("road", "road.rd1"));
        assertFalse(isLocked("road", "road.rd2"));
        assertFalse(isLocked("road", "road.rd3"));

        road1.lockFeatures(rd1Filter);
        assertTrue(isLocked("road", "road.rd1"));
        assertFalse(isLocked("road", "road.rd2"));
        assertFalse(isLocked("road", "road.rd3"));

        road2.lockFeatures(rd2Filter);
        assertTrue(isLocked("road", "road.rd1"));
        assertTrue(isLocked("road", "road.rd2"));
        assertFalse(isLocked("road", "road.rd3"));

        try {
            road1.unLockFeatures(rd1Filter);
            fail("need authorization");
        } catch (IOException expected) {
        }
        t1.addAuthorization(lockA.getAuthorization());
        try {
            road1.unLockFeatures(rd2Filter);
            fail("need correct authorization");
        } catch (IOException expected) {
        }
        road1.unLockFeatures(rd1Filter);
        assertFalse(isLocked("road", "road.rd1"));
        assertTrue(isLocked("road", "road.rd2"));
        assertFalse(isLocked("road", "road.rd3"));

        t2.addAuthorization(lockB.getAuthorization());
        road2.unLockFeatures(rd2Filter);
        assertFalse(isLocked("road", "road.rd1"));
        assertFalse(isLocked("road", "road.rd2"));
        assertFalse(isLocked("road", "road.rd3"));
    }
 public void testGetFeatureLockingExpire() throws Exception {
        FeatureLock lock = FeatureLockFactory.generate("Timed", 500);        
        FeatureLocking<SimpleFeatureType, SimpleFeature> road = 
(FeatureLocking<SimpleFeatureType, SimpleFeature>) 
data.getFeatureSource("road");
        road.setFeatureLock(lock);
        assertFalse(isLocked("road", "road.rd1"));
        road.lockFeatures(rd1Filter);
        assertTrue(isLocked("road", "road.rd1"));
        long then = System.currentTimeMillis();
        do {
            Thread.sleep( 15 );            
        } while ( then > System.currentTimeMillis() - 515 );     
        assertFalse(isLocked("road", "road.rd1"));
    }

On 08/09/2010, at 12:19 AM, Augusttown wrote:

> 
> Jody,
> 
> Is there a JUnit test or example written for testing
> FeatureLock.TRANSACTION? There seems to be a problem when using
> FeatureLock.TRANSACTION to lock and modify features. There is always a
> java.lang.IllegalMonitorStateException exception being thrown out when the
> release() is called on instance of TransactionLock. The change has actually
> been committed to the database already before the exception.
> 
> I'm kind of stuck here, and I don't think there is any JUnit tests or sample
> written using FeatureLock.TRANSACTION. Will source code of GeoServer shed
> any light on this?
> 
> The code below will reproduce the issue:
> 
> for(int i=0; i<4; i++) {
>            // spread a new thread
>            final String id = String.valueOf(i);
>            threadPool.execute(
>                new Runnable() {                   
>                    public void run() {
>                        try {
>                            String targetFeatureTypeName =
> "sf_pizzastores_wgs84";       
>                            JDBCFeatureStore jdbcFeatureStore =
> (JDBCFeatureStore)jdbcDataStore.getFeatureSource(targetFeatureTypeName);      
>                                                                
>                            Transaction transaction = new
> DefaultTransaction();                                                         
>                              
>                            FeatureLock featureLock =
> FeatureLock.TRANSACTION;                                                      
>             
>                            String lockId = featureLock.getAuthorization();
>                            jdbcFeatureStore.setTransaction(transaction);      
>                      
>                            jdbcFeatureStore.setFeatureLock(featureLock);      
>                      
>                            transaction.addAuthorization(lockId);              
>                                          
>                            FeatureWriter<SimpleFeatureType, SimpleFeature>
> featureWriter = jdbcDataStore.getFeatureWriter(targetFeatureTypeName,
> transaction);                                                               
>                            jdbcFeatureStore.lockFeatures();                   
>                                
>                            while(featureWriter.hasNext()) {               
>                                SimpleFeature simpleFeature =
> featureWriter.next();               
>                                String store_id =
> (String)simpleFeature.getAttribute("store_id");
>                                simpleFeature.setAttribute("name", "ps"+ id
> + "." + store_id);                                                           
>                                transaction.addAuthorization(lockId);          
>                      
>                                featureWriter.write();                         
>       
>                            }
>                            try {                                              
>                                  
>                                transaction.addAuthorization(lockId);          
>                                  
>                                transaction.commit();                          
>  
>                            } catch(Exception e) {                             
>                                                   
>                                 transaction.rollback();
>                                 e.printStackTrace();
>                            } finally {           
>                                transaction.addAuthorization(lockId);          
>                                                      
>                                jdbcFeatureStore.unLockFeatures();             
>                                   
>                                featureWriter.close();
>                                transaction.close();
>                            }                   
>                        } catch(Exception e) {                               
>                            e.printStackTrace();
>                        } finally {
>                        }
>                    }
>                }
>            );   
>        }
>        threadPool.shutdown();
> -- 
> View this message in context: 
> http://osgeo-org.1803224.n2.nabble.com/How-to-use-FeatureLock-TRANSACTION-when-modifying-features-and-committing-changes-tp5491953p5508478.html
> Sent from the geotools-gt2-users mailing list archive at Nabble.com.
> 
> ------------------------------------------------------------------------------
> This SF.net Dev2Dev email is sponsored by:
> 
> Show off your parallel programming skills.
> Enter the Intel(R) Threading Challenge 2010.
> http://p.sf.net/sfu/intel-thread-sfd
> _______________________________________________
> Geotools-gt2-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to