From: <one...@us...> - 2002-11-26 03:36:16
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/odmg In directory sc8-pr-cvs1:/tmp/cvs-serv20166/cirrus/hibernate/odmg Modified Files: Database.java Implementation.java Name.java OQLQuery.java Transaction.java Log Message: fixed broken line-endings and added a test Index: Database.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/odmg/Database.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Database.java 26 Oct 2002 09:39:47 -0000 1.6 --- Database.java 26 Nov 2002 03:35:43 -0000 1.7 *************** *** 1,9 **** //$Id$ ! package cirrus.hibernate.odmg; ! import java.sql.SQLException; import org.odmg.ODMGException; import org.odmg.ODMGRuntimeException; import org.odmg.ObjectNameNotFoundException; ! import org.odmg.ObjectNameNotUniqueException; import cirrus.hibernate.Hibernate; import cirrus.hibernate.HibernateException; --- 1,9 ---- //$Id$ ! package cirrus.hibernate.odmg; ! import java.sql.SQLException; import org.odmg.ODMGException; import org.odmg.ODMGRuntimeException; import org.odmg.ObjectNameNotFoundException; ! import org.odmg.ObjectNameNotUniqueException; import cirrus.hibernate.Hibernate; import cirrus.hibernate.HibernateException; *************** *** 11,15 **** import cirrus.hibernate.Session; import cirrus.hibernate.SessionFactory; ! import cirrus.hibernate.impl.SessionFactoryObjectFactory; /** --- 11,15 ---- import cirrus.hibernate.Session; import cirrus.hibernate.SessionFactory; ! import cirrus.hibernate.impl.SessionFactoryObjectFactory; /** *************** *** 28,32 **** * @see Transaction */ ! public class Database implements org.odmg.Database { private SessionFactory sessionFactory; private final ThreadLocal threadTransaction = new ThreadLocal(); --- 28,32 ---- * @see Transaction */ ! public class Database implements org.odmg.Database { private SessionFactory sessionFactory; private final ThreadLocal threadTransaction = new ThreadLocal(); *************** *** 47,51 **** * Get the <tt>Session</tt> underlying the <tt>Transaction</tt> associated * with the current thread. ! */ public Session getSession() { return currentTransaction().getSession(); --- 47,51 ---- * Get the <tt>Session</tt> underlying the <tt>Transaction</tt> associated * with the current thread. ! */ public Session getSession() { return currentTransaction().getSession(); *************** *** 58,62 **** return sessionFactory; } ! /** * Get the <tt>Transaction</tt> associatedm with the current thread. --- 58,62 ---- return sessionFactory; } ! /** * Get the <tt>Transaction</tt> associatedm with the current thread. *************** *** 64,71 **** public Transaction currentTransaction() { return (Transaction) threadTransaction.get(); ! } void associateThread(Transaction trans) { threadTransaction.set(trans); ! } void disassociateThread() { threadTransaction.set(null); --- 64,71 ---- public Transaction currentTransaction() { return (Transaction) threadTransaction.get(); ! } void associateThread(Transaction trans) { threadTransaction.set(trans); ! } void disassociateThread() { threadTransaction.set(null); *************** *** 75,79 **** * @see org.odmg.Database#open(String, int) */ ! public void open(String name, int accessMode) throws ODMGException { try { Hibernate.configure(); --- 75,79 ---- * @see org.odmg.Database#open(String, int) */ ! public void open(String name, int accessMode) throws ODMGException { try { Hibernate.configure(); *************** *** 89,95 **** catch (NamingException ne) { throw new ODMGException( ne.getMessage() ); ! }*/ } ! /** * Close the <tt>Database</tt> (but not the underlying <tt>SessionFactory</tt>). --- 89,95 ---- catch (NamingException ne) { throw new ODMGException( ne.getMessage() ); ! }*/ } ! /** * Close the <tt>Database</tt> (but not the underlying <tt>SessionFactory</tt>). *************** *** 101,105 **** sessionFactory = null; //TODO: remove it from ThreadLocal storage on Implementation ! } /** --- 101,105 ---- sessionFactory = null; //TODO: remove it from ThreadLocal storage on Implementation ! } /** *************** *** 107,115 **** * @see org.odmg.Database#bind(Object, String) */ ! public void bind(Object object, String name) throws ObjectNameNotUniqueException { ! try { Session s = getSession(); Name nameObj = new Name( name, object.getClass(), s.save(object) ); ! s.save(nameObj); //TODO: handle ObjectNameNotUniqueException properly } --- 107,115 ---- * @see org.odmg.Database#bind(Object, String) */ ! public void bind(Object object, String name) throws ObjectNameNotUniqueException { ! try { Session s = getSession(); Name nameObj = new Name( name, object.getClass(), s.save(object) ); ! s.save(nameObj); //TODO: handle ObjectNameNotUniqueException properly } *************** *** 119,132 **** catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); ! } ! } ! /** * Retrieve the persistent object bound to the given name. * @see org.odmg.Database#lookup(String) */ ! public Object lookup(String name) throws ObjectNameNotFoundException { ! try { ! Session s = getSession(); Name nameObj; try { --- 119,132 ---- catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); ! } ! } ! /** * Retrieve the persistent object bound to the given name. * @see org.odmg.Database#lookup(String) */ ! public Object lookup(String name) throws ObjectNameNotFoundException { ! try { ! Session s = getSession(); Name nameObj; try { *************** *** 136,140 **** throw new ObjectNameNotFoundException(); } ! return s.load( nameObj.getPersistentClass(), nameObj.getId() ); } catch (HibernateException he) { --- 136,140 ---- throw new ObjectNameNotFoundException(); } ! return s.load( nameObj.getPersistentClass(), nameObj.getId() ); } catch (HibernateException he) { *************** *** 143,148 **** catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); ! } ! } /** --- 143,148 ---- catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); ! } ! } /** *************** *** 150,160 **** * @see org.odmg.Database#unbind(String) */ ! public void unbind(String name) throws ObjectNameNotFoundException { ! try { ! Session s = getSession(); ! Name nameObj; try { nameObj = (Name) s.load(Name.class, name); } catch (ObjectNotFoundException onfe) { throw new ObjectNameNotFoundException(); } s.delete(nameObj); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); ! } ! } /** --- 150,160 ---- * @see org.odmg.Database#unbind(String) */ ! public void unbind(String name) throws ObjectNameNotFoundException { ! try { ! Session s = getSession(); ! Name nameObj; try { nameObj = (Name) s.load(Name.class, name); } catch (ObjectNotFoundException onfe) { throw new ObjectNameNotFoundException(); } s.delete(nameObj); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); ! } ! } /** *************** *** 162,166 **** * @see org.odmg.Database#makePersistent(Object) */ ! public void makePersistent(Object object) { try { getSession().save(object); --- 162,166 ---- * @see org.odmg.Database#makePersistent(Object) */ ! public void makePersistent(Object object) { try { getSession().save(object); *************** *** 171,181 **** catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); ! } ! } /** * Delete the given object from the database. * @see org.odmg.Database#deletePersistent(Object) */ ! public void deletePersistent(Object object) { try { getSession().delete(object); --- 171,181 ---- catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); ! } ! } /** * Delete the given object from the database. * @see org.odmg.Database#deletePersistent(Object) */ ! public void deletePersistent(Object object) { try { getSession().delete(object); *************** *** 186,191 **** catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); ! } ! } ! ! } --- 186,191 ---- catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); ! } ! } ! ! } Index: Implementation.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/odmg/Implementation.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** Implementation.java 5 Nov 2002 16:58:14 -0000 1.8 --- Implementation.java 26 Nov 2002 03:35:43 -0000 1.9 *************** *** 1,4 **** //$Id$ ! package cirrus.hibernate.odmg; import java.util.ArrayList; import java.util.HashMap; --- 1,4 ---- //$Id$ ! package cirrus.hibernate.odmg; import java.util.ArrayList; import java.util.HashMap; *************** *** 11,15 **** import org.odmg.DSet; import org.odmg.ODMGException; ! import org.odmg.ODMGRuntimeException; import cirrus.hibernate.HibernateException; import cirrus.hibernate.collections.Bag; --- 11,15 ---- import org.odmg.DSet; import org.odmg.ODMGException; ! import org.odmg.ODMGRuntimeException; import cirrus.hibernate.HibernateException; import cirrus.hibernate.collections.Bag; *************** *** 17,21 **** import cirrus.hibernate.collections.Map; import cirrus.hibernate.collections.Set; ! import cirrus.hibernate.engine.SessionImplementor; /** * Singleton class implementing the ODMG <tt>Implementation</tt> interface. --- 17,21 ---- import cirrus.hibernate.collections.Map; import cirrus.hibernate.collections.Set; ! import cirrus.hibernate.engine.SessionImplementor; /** * Singleton class implementing the ODMG <tt>Implementation</tt> interface. *************** *** 24,28 **** */ public class Implementation implements org.odmg.Implementation { ! private Database database; private final ThreadLocal threadDatabase = new ThreadLocal(); --- 24,28 ---- */ public class Implementation implements org.odmg.Implementation { ! private Database database; private final ThreadLocal threadDatabase = new ThreadLocal(); *************** *** 34,38 **** public static Implementation getInstance() { return INSTANCE; ! } /** * Instantiate a new <tt>Transaction</tt> associated with the current --- 34,38 ---- public static Implementation getInstance() { return INSTANCE; ! } /** * Instantiate a new <tt>Transaction</tt> associated with the current *************** *** 61,65 **** return database; } ! } /** * Get the <tt>Transaction</tt> associated with the current thread. --- 61,65 ---- return database; } ! } /** * Get the <tt>Transaction</tt> associated with the current thread. *************** *** 68,72 **** public org.odmg.Transaction currentTransaction() { return currentDatabase().currentTransaction(); ! } /** * Create a new <tt>Database</tt> and associate it with the current thread. --- 68,72 ---- public org.odmg.Transaction currentTransaction() { return currentDatabase().currentTransaction(); ! } /** * Create a new <tt>Database</tt> and associate it with the current thread. *************** *** 81,85 **** threadDatabase.set(db); return db; ! } /** * Instantiate an <tt>OQLQuery</tt> for the <tt>Database</tt> associated with --- 81,85 ---- threadDatabase.set(db); return db; ! } /** * Instantiate an <tt>OQLQuery</tt> for the <tt>Database</tt> associated with *************** *** 89,93 **** public org.odmg.OQLQuery newOQLQuery() { return new OQLQuery( currentDatabase() ); ! } /** * @see org.odmg.Implementation#newDList() --- 89,93 ---- public org.odmg.OQLQuery newOQLQuery() { return new OQLQuery( currentDatabase() ); ! } /** * @see org.odmg.Implementation#newDList() *************** *** 95,99 **** public DList newDList() { return new List( (SessionImplementor) currentDatabase().getSession(), new ArrayList() ); ! } /** * @see org.odmg.Implementation#newDBag() --- 95,99 ---- public DList newDList() { return new List( (SessionImplementor) currentDatabase().getSession(), new ArrayList() ); ! } /** * @see org.odmg.Implementation#newDBag() *************** *** 101,105 **** public DBag newDBag() { return new Bag( (SessionImplementor) currentDatabase().getSession(), new ArrayList() ); ! } /** * @see org.odmg.Implementation#newDSet() --- 101,105 ---- public DBag newDBag() { return new Bag( (SessionImplementor) currentDatabase().getSession(), new ArrayList() ); ! } /** * @see org.odmg.Implementation#newDSet() *************** *** 107,111 **** public DSet newDSet() { return new Set( (SessionImplementor) currentDatabase().getSession(), new HashSet() ); ! } /** * @see org.odmg.Implementation#newDArray() --- 107,111 ---- public DSet newDSet() { return new Set( (SessionImplementor) currentDatabase().getSession(), new HashSet() ); ! } /** * @see org.odmg.Implementation#newDArray() *************** *** 113,117 **** public DArray newDArray() { return new List( (SessionImplementor) currentDatabase().getSession(), new ArrayList() ); ! } /** * @see org.odmg.Implementation#newDMap() --- 113,117 ---- public DArray newDArray() { return new List( (SessionImplementor) currentDatabase().getSession(), new ArrayList() ); ! } /** * @see org.odmg.Implementation#newDMap() *************** *** 119,128 **** public DMap newDMap() { return new Map( (SessionImplementor) currentDatabase().getSession(), new HashMap() ); ! } /** * Get the stringified identifier of the given object. * @see org.odmg.Implementation#getObjectId(Object) */ ! public String getObjectId(Object obj) { try { return database.getSession().getIdentifier(obj).toString(); --- 119,128 ---- public DMap newDMap() { return new Map( (SessionImplementor) currentDatabase().getSession(), new HashMap() ); ! } /** * Get the stringified identifier of the given object. * @see org.odmg.Implementation#getObjectId(Object) */ ! public String getObjectId(Object obj) { try { return database.getSession().getIdentifier(obj).toString(); *************** *** 130,135 **** catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); ! } ! } /** * Get the <tt>Database</tt> associated with the current thread (the most recent --- 130,135 ---- catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); ! } ! } /** * Get the <tt>Database</tt> associated with the current thread (the most recent *************** *** 141,144 **** return currentDatabase(); } ! ! } --- 141,144 ---- return currentDatabase(); } ! ! } Index: Name.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/odmg/Name.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** Name.java 11 Sep 2002 09:39:53 -0000 1.2 --- Name.java 26 Nov 2002 03:35:43 -0000 1.3 *************** *** 1,15 **** ! //$Id$ package cirrus.hibernate.odmg; ! import java.io.Serializable; ! /** * An ODMG name that may be bound to a persistent object. */ public class Name { ! private String name; private Class persistentClass; private Serializable id; ! public Name(String name, Class persistentClass, Serializable id) { this.name = name; this.persistentClass = persistentClass; this.id = id; } ! public Name() {} ! /** * Returns the name. * @return String */ ! public String getName() { return name; } ! /** * Returns the persistentClass. * @return Class */ public Class getPersistentClass() { return persistentClass; } ! /** * Sets the name. * @param name The name to set */ public void setName(String name) { this.name = name; } ! /** * Sets the persistentClass. * @param persistentClass The persistentClass to set */ public void setPersistentClass(Class persistentClass) { this.persistentClass = persistentClass; } ! /** * Returns the id. * @return Serializable */ public Serializable getId() { return id; } ! /** * Sets the id. * @param id The id to set */ public void setId(Serializable id) { this.id = id; } ! } --- 1,15 ---- ! //$Id$ package cirrus.hibernate.odmg; ! import java.io.Serializable; ! /** * An ODMG name that may be bound to a persistent object. */ public class Name { ! private String name; private Class persistentClass; private Serializable id; ! public Name(String name, Class persistentClass, Serializable id) { this.name = name; this.persistentClass = persistentClass; this.id = id; } ! public Name() {} ! /** * Returns the name. * @return String */ ! public String getName() { return name; } ! /** * Returns the persistentClass. * @return Class */ public Class getPersistentClass() { return persistentClass; } ! /** * Sets the name. * @param name The name to set */ public void setName(String name) { this.name = name; } ! /** * Sets the persistentClass. * @param persistentClass The persistentClass to set */ public void setPersistentClass(Class persistentClass) { this.persistentClass = persistentClass; } ! /** * Returns the id. * @return Serializable */ public Serializable getId() { return id; } ! /** * Sets the id. * @param id The id to set */ public void setId(Serializable id) { this.id = id; } ! } Index: OQLQuery.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/odmg/OQLQuery.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** OQLQuery.java 12 Sep 2002 18:19:15 -0000 1.4 --- OQLQuery.java 26 Nov 2002 03:35:43 -0000 1.5 *************** *** 1,11 **** ! //$Id$ package cirrus.hibernate.odmg; ! import java.sql.SQLException; ! import org.odmg.ODMGRuntimeException; import org.odmg.QueryException; import org.odmg.QueryInvalidException; import org.odmg.QueryParameterCountInvalidException; import org.odmg.QueryParameterTypeInvalidException; ! import cirrus.hibernate.HibernateException; import cirrus.hibernate.Query; ! /** Experimental implementation of the ODMG <tt>OQLQuery</tt> interface. The supported query language is actually the Hibernate query language and the <tt>execute()</tt> method returns results in the same format as <tt>Session.find()</tt>.<br> <br> Warning: this implementation will change significantly as ODMG support matures! */ public class OQLQuery implements org.odmg.OQLQuery { ! private Transaction tx; private Query query; private int param=0; /** * Instantiate an <tt>OQLQuery</tt> for the current transaction. */ ! public OQLQuery(Database db) { this.tx = db.currentTransaction(); } ! /** * Instantiate an <tt>OQLQuery</tt> for the given transaction. */ public OQLQuery(Transaction tx) { this.tx = tx; } /** * Instantiate an <tt>OQLQuery</tt> for the current transaction. */ public OQLQuery() { this.tx = (Transaction) Implementation.getInstance().currentTransaction(); } /** * Get the underlying Hibernate <tt>Query</tt>. */ public Query getQuery() { return query; } /** * Set the HIbernate query string. Scalar return values are not supported. * @see org.odmg.OQLQuery#create(String) */ public void create(String queryString) throws QueryInvalidException { //TODO: the right exception try { this.query = tx.getSession().createQuery(queryString); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } } ! /** * Bind a value to the next <tt>?</tt> style parameter. * @see org.odmg.OQLQuery#bind(Object) */ public void bind(Object parameter) throws QueryParameterCountInvalidException, QueryParameterTypeInvalidException { //TODO: the right exception try { query.setParameter(param++, parameter); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } } ! /** * Get the query results as a collection. * @see org.odmg.OQLQuery#execute() */ public Object execute() throws QueryException { //TODO: how are results meant to be returned in ODMG? try { return query.list(); } catch (SQLException sqle) { throw new QueryException( sqle.getMessage() ); } catch (HibernateException he) { throw new QueryException( he.getMessage() ); } } ! } --- 1,11 ---- ! //$Id$ package cirrus.hibernate.odmg; ! import java.sql.SQLException; ! import org.odmg.ODMGRuntimeException; import org.odmg.QueryException; import org.odmg.QueryInvalidException; import org.odmg.QueryParameterCountInvalidException; import org.odmg.QueryParameterTypeInvalidException; ! import cirrus.hibernate.HibernateException; import cirrus.hibernate.Query; ! /** Experimental implementation of the ODMG <tt>OQLQuery</tt> interface. The supported query language is actually the Hibernate query language and the <tt>execute()</tt> method returns results in the same format as <tt>Session.find()</tt>.<br> <br> Warning: this implementation will change significantly as ODMG support matures! */ public class OQLQuery implements org.odmg.OQLQuery { ! private Transaction tx; private Query query; private int param=0; /** * Instantiate an <tt>OQLQuery</tt> for the current transaction. */ ! public OQLQuery(Database db) { this.tx = db.currentTransaction(); } ! /** * Instantiate an <tt>OQLQuery</tt> for the given transaction. */ public OQLQuery(Transaction tx) { this.tx = tx; } /** * Instantiate an <tt>OQLQuery</tt> for the current transaction. */ public OQLQuery() { this.tx = (Transaction) Implementation.getInstance().currentTransaction(); } /** * Get the underlying Hibernate <tt>Query</tt>. */ public Query getQuery() { return query; } /** * Set the HIbernate query string. Scalar return values are not supported. * @see org.odmg.OQLQuery#create(String) */ public void create(String queryString) throws QueryInvalidException { //TODO: the right exception try { this.query = tx.getSession().createQuery(queryString); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } } ! /** * Bind a value to the next <tt>?</tt> style parameter. * @see org.odmg.OQLQuery#bind(Object) */ public void bind(Object parameter) throws QueryParameterCountInvalidException, QueryParameterTypeInvalidException { //TODO: the right exception try { query.setParameter(param++, parameter); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } } ! /** * Get the query results as a collection. * @see org.odmg.OQLQuery#execute() */ public Object execute() throws QueryException { //TODO: how are results meant to be returned in ODMG? try { return query.list(); } catch (SQLException sqle) { throw new QueryException( sqle.getMessage() ); } catch (HibernateException he) { throw new QueryException( he.getMessage() ); } } ! } Index: Transaction.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/odmg/Transaction.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** Transaction.java 5 Nov 2002 16:58:14 -0000 1.6 --- Transaction.java 26 Nov 2002 03:35:43 -0000 1.7 *************** *** 1,17 **** ! //$Id$ package cirrus.hibernate.odmg; ! import java.sql.SQLException; ! import org.odmg.LockNotGrantedException; import org.odmg.ODMGException; import org.odmg.ODMGRuntimeException; ! import cirrus.hibernate.HibernateException; import cirrus.hibernate.LockMode; import cirrus.hibernate.Session; ! /** * Implements the ODMG <tt>Transaction</tt> API. */ public class Transaction implements org.odmg.Transaction { ! private final Database database; private final Session session; private cirrus.hibernate.Transaction tx; ! /** * Instantiate a <tt>Transaction</tt> for the given <tt>Database</tt>. */ public Transaction(org.odmg.Database database) throws ODMGException { this.database = (Database) database; try { this.session = this.database.getSessionFactory().openSession(); } catch (SQLException sqle) { throw new ODMGException( sqle.getMessage() ); } this.database.associateThread(this); } /** * Instantiate a <tt>Transaction</tt> for a <tt>Database</tt> created * by the current thread. */ public Transaction() throws ODMGException { this( Implementation.getInstance().currentDatabase() ); } /** * Get the underlying Hibernate <tt>Session</tt>. (Very useful!) */ ! public Session getSession() { return session; } ! /** * Associate the current thread with this <tt>Transaction</tt> and * disassociate the thread from any other <tt>Transaction</tt>. * @see org.odmg.Transaction#join() */ public void join() { //database.disassociateThread(); database.associateThread(this); } ! /** * Disassociate the thread the <tt>Transaction</tt>. * @see org.odmg.Transaction#leave() */ public void leave() { database.disassociateThread(); } ! /** * Begin the transaction. * @see org.odmg.Transaction#begin() */ public void begin() { try { tx = session.beginTransaction(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } } ! /** * Is the transaction open? Returns true if <tt>begin()</tt> was called but * neither <tt>commit()</tt> nor <tt>abort()</tt> was called. * @see org.odmg.Transaction#isOpen() */ public boolean isOpen() { return tx==null; } ! /** * Commit the transaction. * @see org.odmg.Transaction#commit() */ public void commit() { database.disassociateThread(); try { tx.commit(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); } finally { tx=null; try { session.close(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); } } } ! /** * Abort the transaction. * @see org.odmg.Transaction#abort() */ public void abort() { database.disassociateThread(); try { tx.rollback(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } finally { tx=null; try { session.close(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); } } } ! /** * Commit the changes, but leave the transaction open. This implementation * does not have quite the same semantics os ODMG (locks are not retained). * So you should only use this with versioned data. * @see org.odmg.Transaction#checkpoint() */ public void checkpoint() { try { tx.commit(); tx = session.beginTransaction(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); } } /** * Obtain a lock upon the given object. In the present implementation, * <tt>READ</tt> lock mode is ignored while <tt>UPGRADE</tt> and * <tt>WRITE</tt> lock modes obtain an <tt>UPGRADE</tt> lock for databases * which support <tt>for update</tt>. We should improve this eventually.... * @see org.odmg.Transaction#lock(Object, int) */ public void lock(Object obj, int lockMode) throws LockNotGrantedException { //TODO: check the semantics of this... try { if ( lockMode==org.odmg.Transaction.READ ) { session.lock(obj, LockMode.READ); } else { session.lock(obj, LockMode.UPGRADE); } } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); } } ! /** * Not implemented. * @see org.odmg.Transaction#tryLock(Object, int) */ public boolean tryLock(Object obj, int lockMode) { throw new UnsupportedOperationException("try using lock()"); } ! } --- 1,17 ---- ! //$Id$ package cirrus.hibernate.odmg; ! import java.sql.SQLException; ! import org.odmg.LockNotGrantedException; import org.odmg.ODMGException; import org.odmg.ODMGRuntimeException; ! import cirrus.hibernate.HibernateException; import cirrus.hibernate.LockMode; import cirrus.hibernate.Session; ! /** * Implements the ODMG <tt>Transaction</tt> API. */ public class Transaction implements org.odmg.Transaction { ! private final Database database; private final Session session; private cirrus.hibernate.Transaction tx; ! /** * Instantiate a <tt>Transaction</tt> for the given <tt>Database</tt>. */ public Transaction(org.odmg.Database database) throws ODMGException { this.database = (Database) database; try { this.session = this.database.getSessionFactory().openSession(); } catch (SQLException sqle) { throw new ODMGException( sqle.getMessage() ); } this.database.associateThread(this); } /** * Instantiate a <tt>Transaction</tt> for a <tt>Database</tt> created * by the current thread. */ public Transaction() throws ODMGException { this( Implementation.getInstance().currentDatabase() ); } /** * Get the underlying Hibernate <tt>Session</tt>. (Very useful!) */ ! public Session getSession() { return session; } ! /** * Associate the current thread with this <tt>Transaction</tt> and * disassociate the thread from any other <tt>Transaction</tt>. * @see org.odmg.Transaction#join() */ public void join() { //database.disassociateThread(); database.associateThread(this); } ! /** * Disassociate the thread the <tt>Transaction</tt>. * @see org.odmg.Transaction#leave() */ public void leave() { database.disassociateThread(); } ! /** * Begin the transaction. * @see org.odmg.Transaction#begin() */ public void begin() { try { tx = session.beginTransaction(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } } ! /** * Is the transaction open? Returns true if <tt>begin()</tt> was called but * neither <tt>commit()</tt> nor <tt>abort()</tt> was called. * @see org.odmg.Transaction#isOpen() */ public boolean isOpen() { return tx==null; } ! /** * Commit the transaction. * @see org.odmg.Transaction#commit() */ public void commit() { database.disassociateThread(); try { tx.commit(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); } finally { tx=null; try { session.close(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); } } } ! /** * Abort the transaction. * @see org.odmg.Transaction#abort() */ public void abort() { database.disassociateThread(); try { tx.rollback(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } finally { tx=null; try { session.close(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); } } } ! /** * Commit the changes, but leave the transaction open. This implementation * does not have quite the same semantics os ODMG (locks are not retained). * So you should only use this with versioned data. * @see org.odmg.Transaction#checkpoint() */ public void checkpoint() { try { tx.commit(); tx = session.beginTransaction(); } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); } } /** * Obtain a lock upon the given object. In the present implementation, * <tt>READ</tt> lock mode is ignored while <tt>UPGRADE</tt> and * <tt>WRITE</tt> lock modes obtain an <tt>UPGRADE</tt> lock for databases * which support <tt>for update</tt>. We should improve this eventually.... * @see org.odmg.Transaction#lock(Object, int) */ public void lock(Object obj, int lockMode) throws LockNotGrantedException { //TODO: check the semantics of this... try { if ( lockMode==org.odmg.Transaction.READ ) { session.lock(obj, LockMode.READ); } else { session.lock(obj, LockMode.UPGRADE); } } catch (HibernateException he) { throw new ODMGRuntimeException( he.getMessage() ); } catch (SQLException sqle) { throw new ODMGRuntimeException( sqle.getMessage() ); } } ! /** * Not implemented. * @see org.odmg.Transaction#tryLock(Object, int) */ public boolean tryLock(Object obj, int lockMode) { throw new UnsupportedOperationException("try using lock()"); } ! } |