Package org.geotools.data

Examples of org.geotools.data.Transaction


            // we don't have a fast way to perform this delete, let's do it the
            // feature by feature way then
            super.removeFeatures(filter);
        } else {
            // let's grab the connection
            Transaction tx = getState().getTransaction();
            Connection cx = null;
           
            try {
                cx = getDataStore().getConnection(tx);
               
View Full Code Here


        // transactionExample start
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put("directory", directory);
        DataStore store = DataStoreFinder.getDataStore(params);

        Transaction t1 = new DefaultTransaction("transaction 1");
        Transaction t2 = new DefaultTransaction("transactoin 2");

        SimpleFeatureType type = store.getSchema("example");
        SimpleFeatureStore featureStore = (SimpleFeatureStore) store
                .getFeatureSource("example");
        SimpleFeatureStore featureStore1 = (SimpleFeatureStore) store
                .getFeatureSource("example");
        SimpleFeatureStore featureStore2 = (SimpleFeatureStore) store
                .getFeatureSource("example");

        featureStore1.setTransaction(t1);
        featureStore2.setTransaction(t2);
       
        System.out.println("Step 1");
        System.out.println("------");
        System.out.println("start     auto-commit: "+DataUtilities.fidSet(featureStore.getFeatures()) );
        System.out.println("start              t1: "+DataUtilities.fidSet(featureStore1.getFeatures()) );
        System.out.println("start              t2: "+DataUtilities.fidSet(featureStore2.getFeatures()) );
       
        // select feature to remove
        FilterFactory ff = CommonFactoryFinder.getFilterFactory(null);
        Filter filter1 = ff.id(Collections.singleton(ff.featureId("fid1")));
        featureStore1.removeFeatures(filter1); // road1 removes fid1 on t1
       
        System.out.println();
        System.out.println("Step 2 transaction 1 removes feature 'fid1'");
        System.out.println("------");
        System.out.println("t1 remove auto-commit: "+DataUtilities.fidSet(featureStore.getFeatures()) );
        System.out.println("t1 remove          t1: "+DataUtilities.fidSet(featureStore1.getFeatures()) );
        System.out.println("t1 remove          t2: "+DataUtilities.fidSet(featureStore2.getFeatures()) );
        // new feature to add!
        SimpleFeature feature = SimpleFeatureBuilder.build(type, new Object[] {
                5, "chris", null }, "fid5");
        feature.getUserData().put(Hints.USE_PROVIDED_FID,true);
        feature.getUserData().put(Hints.PROVIDED_FID, "fid5");
       
        SimpleFeatureCollection collection = DataUtilities.collection(feature);
        featureStore2.addFeatures(collection);

        System.out.println();
        System.out.println("Step 3 transaction 2 adds a new feature '"+feature.getID()+"'");
        System.out.println("------");
        System.out.println("t2 add    auto-commit: "+DataUtilities.fidSet(featureStore.getFeatures()) );
        System.out.println("t2 add             t1: "+DataUtilities.fidSet(featureStore1.getFeatures()) );
        System.out.println("t1 add             t2: "+DataUtilities.fidSet(featureStore2.getFeatures()) );

        // commit transaction one
        t1.commit();
       
        System.out.println();
        System.out.println("Step 4 transaction 1 commits the removal of feature 'fid1'");
        System.out.println("------");
        System.out.println("t1 commit auto-commit: "+DataUtilities.fidSet(featureStore.getFeatures()) );
        System.out.println("t1 commit          t1: "+DataUtilities.fidSet(featureStore1.getFeatures()) );
        System.out.println("t1 commit          t2: "+DataUtilities.fidSet(featureStore2.getFeatures()) );

        // commit transaction two
        t2.commit();

        System.out.println();
        System.out.println("Step 5 transaction 2 commits the addition of '"+feature.getID()+"'");
        System.out.println("------");
        System.out.println("t2 commit auto-commit: "+DataUtilities.fidSet(featureStore.getFeatures()) );
        System.out.println("t2 commit          t1: "+DataUtilities.fidSet(featureStore1.getFeatures()) );
        System.out.println("t2 commit          t2: "+DataUtilities.fidSet(featureStore2.getFeatures()) );

        t1.close();
        t2.close();
        store.dispose(); // clear out any listeners
        // transactionExample end
        System.out.println("\ntransactionExample end\n");
    }
View Full Code Here

        // removeAllExample start
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        params.put("directory", directory);
        DataStore store = DataStoreFinder.getDataStore(params);

        Transaction t = new DefaultTransaction("transaction");
        try {
            FeatureWriter<SimpleFeatureType, SimpleFeature> writer = store
                    .getFeatureWriter("example", Filter.INCLUDE, t);

            SimpleFeature feature;
            try {
                while (writer.hasNext()) {
                    feature = writer.next();
                    System.out.println("remove " + feature.getID());
                    writer.remove(); // marking contents for removal
                }
            } finally {
                writer.close();
            }
            System.out.println("commit " + t); // only now are the contents
                                               // removed
            t.commit();
        } catch (Throwable eek) {
            t.rollback();
        } finally {
            t.close();
            store.dispose();
        }
        // removeAllExample end
        System.out.println("\nremoveAllExample end\n");
    }
View Full Code Here

        b.set(aname("intProperty"), new Integer(3));
        b.set(aname("geometry"), new GeometryFactory().createPoint(new Coordinate(3, 3)));
        collection.add(b.buildFeature(null));

        FeatureEventWatcher watcher = new FeatureEventWatcher();
        Transaction t = new DefaultTransaction();
        featureStore.setTransaction(t);
        featureStore.addFeatureListener(watcher);
        JDBCFeatureStore featureStore2 = (JDBCFeatureStore) dataStore.getFeatureSource(featureStore.getName().getLocalPart());
        List<FeatureId> fids = featureStore.addFeatures((SimpleFeatureCollection)collection);
       
        assertEquals(1, fids.size());

        // check the store with the transaction sees the new features, but the other store does not
        assertEquals(4, featureStore.getFeatures().size());
        assertEquals(3, featureStore2.getFeatures().size());
       
        // check that after the commit everybody sees 4
        t.commit();
        assertEquals(4, featureStore.getFeatures().size());
        assertEquals(4, featureStore2.getFeatures().size());
       
        t.close();
    }
View Full Code Here

        FeatureEventWatcher watcher = new FeatureEventWatcher();
       
        Connection conn = setup.getDataSource().getConnection();
        conn.setAutoCommit(false);
        Transaction t = dataStore.buildTransaction(conn);
        featureStore.setTransaction(t);
        featureStore.addFeatureListener(watcher);
        JDBCFeatureStore featureStore2 = (JDBCFeatureStore) dataStore.getFeatureSource(featureStore.getName().getLocalPart());
        List<FeatureId> fids = featureStore.addFeatures((SimpleFeatureCollection)collection);
       
        assertEquals(1, fids.size());

        // check the store with the transaction sees the new features, but the other store does not
        assertEquals(4, featureStore.getFeatures().size());
        assertEquals(3, featureStore2.getFeatures().size());
       
        // check that after the commit on the transaction things have not changed,
        // the connection is externally managed
        t.commit();
        assertEquals(4, featureStore.getFeatures().size());
        assertEquals(3, featureStore2.getFeatures().size());

        // commit directly
        conn.commit();
        assertEquals(4, featureStore.getFeatures().size());
        assertEquals(4, featureStore2.getFeatures().size());
       
        // check that closing the transaction does not affect the connection
        t.close();
        assertFalse(conn.isClosed());
        conn.close();
    }
View Full Code Here

      assertEquals( count, visitor.total );
      visitor.total = 0; // reset

      // test on a transaction
      JDBCFeatureStore ft1 = (JDBCFeatureStore) dataStore.getFeatureSource(tname("ft1"));
      Transaction transaction = new DefaultTransaction();
      try {
        ft1.setTransaction( transaction );
        Connection connection = ft1.getDataStore().getConnection( ft1.getState() );
        assertFalse( "connection established", connection.isClosed() );
       
        ft1.accepts( Query.ALL,  visitor, null );
       
        assertFalse( "connection maintained", connection.isClosed() );
      }
      finally {
        if( transaction != null ){
          transaction.close();
        }
      }
     }
View Full Code Here

   
    private void testInsertEmptyGeometry(String type) throws Exception {
        WKTReader reader = new WKTReader();
        Geometry emptyGeometry = reader.read(type.toUpperCase() + " EMPTY");

        Transaction tx = new DefaultTransaction();
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriterAppend(
                tname("empty"), tx);
        SimpleFeature feature = writer.next();
        feature.setAttribute(aname("id"), new Integer(100));
        feature.setAttribute(aname("geom_" + type.toLowerCase()), emptyGeometry);
        feature.setAttribute(aname("name"), new String("empty " + type));
        writer.write();
        writer.close();
        tx.commit();
        tx.close();

        SimpleFeatureCollection fc = dataStore.getFeatureSource(tname("empty")).getFeatures();
        assertEquals(1, fc.size());
        SimpleFeatureIterator fi = fc.features();
        SimpleFeature nf = fi.next();
View Full Code Here

        assertTrue(mockListener.onReleaseCalled);
        assertFalse(mockListener.onCommitCalled);
        assertFalse(mockListener.onRollbackCalled);
       
        // now write something within a transaction
        Transaction t = new DefaultTransaction();
        SimpleFeatureBuilder b = new SimpleFeatureBuilder(featureStore.getSchema());
        DefaultFeatureCollection collection = new DefaultFeatureCollection(null,
                featureStore.getSchema());
        featureStore.setTransaction(t);
        for (int i = 3; i < 6; i++) {
            b.set(aname("intProperty"), new Integer(i));
            b.set(aname("geometry"), new GeometryFactory().createPoint(new Coordinate(i, i)));
            collection.add(b.buildFeature(null));
        }
        featureStore.addFeatures((SimpleFeatureCollection)collection);
        t.commit();
        assertTrue(mockListener.onBorrowCalled);
        assertTrue(mockListener.onReleaseCalled);
        assertTrue(mockListener.onCommitCalled);
        assertFalse(mockListener.onRollbackCalled);
       
        // and now do a rollback
        t.rollback();
        assertTrue(mockListener.onRollbackCalled);
        t.close();
    }
View Full Code Here

    public void testLockFeatures() throws Exception {
       
        FeatureLock lock =
            FeatureLockFactory.generate(tname("ft1"), 60 * 60 * 1000);
       
        Transaction tx = new DefaultTransaction();
        store.setTransaction( tx );
        store.setFeatureLock(lock);
       
        //lock features
        int locked = store.lockFeatures();
        assertTrue( locked > 0 );
       
        //grabbing a reader should be no problem
        DefaultQuery query = new DefaultQuery( tname("ft1") );
        FeatureReader<SimpleFeatureType, SimpleFeature> reader = dataStore.getFeatureReader(query,tx);
       
        int count = 0;
        while( reader.hasNext() ) {
            count++;
            reader.next();
        }
        assertTrue( count > 0 );
        reader.close();
       
        //grab a writer
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriter(tname("ft1"), tx );
        assertTrue( writer.hasNext() );
        SimpleFeature feature = writer.next();
       
        feature.setAttribute(aname("intProperty"), new Integer(100) );
        try {
            writer.write()
            fail( "should have thrown feature lock exception" );
        }
        catch( FeatureLockException e ) {
            //good
        }
        finally {
            writer.close();
        }
       
        tx.addAuthorization(lock.getAuthorization());
        writer = dataStore.getFeatureWriter(tname("ft1"), tx);
        assertTrue( writer.hasNext() );
        feature = writer.next();
        feature.setAttribute(aname("intProperty"), new Integer(100) );
        writer.write();
        writer.close();
        tx.close();
    }
View Full Code Here

    public void testLockFeaturesWithFilter() throws Exception {
       
        FeatureLock lock =
            FeatureLockFactory.generate(tname("ft1"), 60 * 60 * 1000);
       
        Transaction tx = new DefaultTransaction();
        store.setTransaction( tx );
        store.setFeatureLock(lock);
       
        //lock features
        FilterFactory ff = dataStore.getFilterFactory();
        PropertyIsEqualTo f = ff.equals(ff.property(aname("intProperty")), ff.literal(1));
       
        int locked = store.lockFeatures( f );
        assertEquals( 1, locked );
       
        //grabbing a reader should be no problem
        FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriter(tname("ft1"), tx);
        boolean failure = false;
        while( writer.hasNext() ) {
            SimpleFeature feature = (SimpleFeature) writer.next();
            Number old = (Number) feature.getAttribute(aname("intProperty"));
           
            feature.setAttribute(aname("intProperty"), new Integer(100));
            if ( new Integer(1).equals( old.intValue() ) ) {
                try {
                    writer.write();
                    fail( "writer should have thrown exception for locked feature");
                }
                catch( FeatureLockException e ) {
                    failure = true;
                }
            }
            else {
                writer.write();
            }
        }
        writer.close();
       
        assertTrue( failure );
        tx.close();
    }
View Full Code Here

TOP

Related Classes of org.geotools.data.Transaction

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.