Tamino XML Server Version 9.7
 —  Transactions Guide  —

Appendix 1 - Complete Java Code example of Scenario 1

The following code represents a complete sample program written in Java and intended for use with the Tamino API for Java for scenario 1.

import java.io.*;

import com.softwareag.tamino.db.api.connection.*;
import com.softwareag.tamino.db.api.accessor.*;
import com.softwareag.tamino.db.api.objectModel.*;
import com.softwareag.tamino.db.api.response.*;
import com.softwareag.tamino.db.api.common.*;
import com.softwareag.tamino.db.api.objectModel.dom.TDOMObjectModel;
import org.w3c.dom.Element;

/**
 * Transaction scenario 01: 
 * read only access without consistency requirements using a cursor
 */
public class TxScenario01b
{
	public static void main(String[] args) throws Exception
	{
		TConnection connection = null;
		String taminoURI = "http://localhost/tamino/myDB";
		String collection = "encyclopedia";
		try
		{
			// Establish the Tamino connection (by default in auto-commit mode)
			connection = TConnectionFactory.getInstance().newConnection( taminoURI );

			// Establish a transaction context and therefore create a session
			TLocalTransaction transaction = connection.useLocalTransactionMode();
			connection.setIsolationDegree(TIsolationDegree.UNCOMMITTED_DOCUMENT);

			// Obtain a TXMLObjectAccessor with a DOM object model
			TXMLObjectAccessor accessor =
				connection.newXMLObjectAccessor(
					TAccessLocation.newInstance( collection ),
					TDOMObjectModel.getInstance() );

			// Optionally set the lock mode to unprotected to avoid any locks
			accessor.setLockMode(TLockMode.UNPROTECTED);

			// execute a query
			TQuery query = 
   TQuery.newInstance("/jazzMusician[./instrument=\"trumpet\"]/name/first");
			TResponse response = accessor.query(query,5);

			// display result
			TXMLObjectIterator xmlObjectIterator = response.getXMLObjectIterator();
			while (xmlObjectIterator.hasNext()) {
				TXMLObject xmlObject = xmlObjectIterator.next();
				System.out.println("result: " 
+ ((Element)xmlObject.getElement()).getFirstChild().getNodeValue());
			}
		}
		catch (TException e)
		{
			System.err.println("Tamino Exception caught: " + e);
		}
		finally
		{
			if (connection != null)	connection.close();
		}
	}
}

Top of page