// TransactionsExample.java // // o Gets a the root collection of the Sedna XML Database. // o Creates a collection called myCollection. // o Starts a Transaction (gets out of auto-commit mode). // o Inserts 50 random XML Documents in collection myCollection. // o Randomly performs either commit or rollback. // o Optionally shows the list of resources in the collection. // o Deletes the collection myCollection. // o Closes the root collection. import org.xmldb.api.*; import org.xmldb.api.base.*; import org.xmldb.api.modules.*; public class TransactionsExample { // -------------------- // Change these details // -------------------- private String databaseURI = "xmldb:sedna://localhost/db"; private String databaseUsername = "SYSTEM"; private String databasePassword = "MANAGER"; // -------------------- public static void main(String[] args) { TransactionsExample te = new TransactionsExample(); te.execute(); } public void execute() { try { registerXMLDBDriver(); Collection rootCollection = DatabaseManager.getCollection(databaseURI, databaseUsername, databasePassword); Collection myCollection = createChildCollection(rootCollection, "myCollection"); TransactionService transactionService = (TransactionService)myCollection.getService( "TransactionService","1.0"); transactionService.begin(); // begin acid transction for(int i=0;i<50;i++) { String xmlDocument = "<myDoc>\n" + "<id>" + i + "</id>\n" + "<data>" + (Math.random()*1000000) + "</data>\n" + "</myDoc>"; XMLResource resource = (XMLResource)myCollection.createResource(null, XMLResource.RESOURCE_TYPE); resource.setContent(xmlDocument); myCollection.storeResource(resource); } boolean willCommit = Math.round((float)Math.random()) == 1; if(willCommit) { transactionService.commit(); // Performed a "Commit" on the Transaction this time. } else { transactionService.rollback(); // Performed a "Rollback" on the Transaction this time. } // Now back in auto-commit mode. int numberOfDocuments = myCollection.getResourceCount(); if(numberOfDocuments == 0) { System.out.println( "Collection is empty. Looks like rollback was successful."); } else { String[] resources = myCollection.listResources(); System.out.println( "50 XML documents exist within collection 'myCollection':"); for(int x=0;x<resources.length;x++) System.out.println("doc["+x+"] = \""+resources[x]+"\"."); } deleteCollection(rootCollection, "myCollection"); rootCollection.close(); } catch(XMLDBException e) { e.printStackTrace(); } } public void registerXMLDBDriver() throws XMLDBException { try { Database dbDriver = (Database)Class.forName( "net.cfoster.sedna.DatabaseImpl").newInstance(); DatabaseManager.registerDatabase(dbDriver); } catch(ClassNotFoundException e) { System.err.println("ClassNotFoundException: "+ e.getMessage()); } catch(InstantiationException e) { System.err.println("InstantiationException: "+ e.getMessage()); } catch(IllegalAccessException e) { System.err.println("IllegalAccessException: "+ e.getMessage()); } } public Collection createChildCollection(Collection collection, String collectionName) throws XMLDBException { CollectionManagementService cms = (CollectionManagementService)collection.getService( "CollectionManagementService","1.0"); return cms.createCollection(collectionName); } public void deleteCollection(Collection collection, String collectionName) throws XMLDBException CollectionManagementService cms = (CollectionManagementService)collection.getService( "CollectionManagementService","1.0"); cms.removeCollection(collectionName); } }