用Java实现的23个常用设计模式源代码

源代码在线查看: database.java

软件大小: 12795 K
上传用户: haorenkv3
关键词: Java 设计模式 源代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				//$Id: Database.java,v 1.5 2003/01/26 01:33:36 oneovthafew Exp $				package net.sf.hibernate.odmg;								import org.odmg.ODMGException;				import org.odmg.ODMGRuntimeException;				import org.odmg.ObjectNameNotFoundException;				import org.odmg.ObjectNameNotUniqueException;								import net.sf.hibernate.HibernateException;				import net.sf.hibernate.ObjectNotFoundException;				import net.sf.hibernate.Session;				import net.sf.hibernate.SessionFactory;				import net.sf.hibernate.cfg.Configuration;				import net.sf.hibernate.impl.SessionFactoryObjectFactory;								/**				 * Implements the ODMG Database API. This provides operations for				 * persisting and deleteing objects, binding names to objects and looking				 * up objects by name.				 * 				 * Applications may create an instance by calling				 * Implementation.newDatabase() or by instantiating this class				 * directly. The application should then call open() to associate				 * it with an existing Hibernate SessionFactory.				 * 				 * Instances maintain an association between Threads and				 * Transactions.				 * @see Implementation				 * @see Transaction				 */				public class Database implements org.odmg.Database {									private SessionFactory sessionFactory;					private final ThreadLocal threadTransaction = new ThreadLocal();										/**					 * Instantiate					 */					public Database() {}										/**					 * Specify the underlying SessionFactory. This method is not					 * exposed by the ODMG API but is needed when no JNDI server is available.					 */					public void open(SessionFactory factory) {						this.sessionFactory=factory;					}										/**					 * Get the Session underlying the Transaction associated					 * with the current thread.					 */					public Session getSession() {						return currentTransaction().getSession();					}										/**					 * Get the SessionFactory underlying this Database.					 */					public SessionFactory getSessionFactory() {						return sessionFactory;					}										/**					 * Get the Transaction associatedm with the current thread.					 */					public Transaction currentTransaction() {						return (Transaction) threadTransaction.get();					}					void associateThread(Transaction trans) {						threadTransaction.set(trans);					}					void disassociateThread() {						threadTransaction.set(null);					}										/**					 * Specify the underlying SessionFactory, by passing a JNDI name.					 * The accessMode is ignored by Hibernate.					 * @see org.odmg.Database#open(String, int)					 */					public void open(String name, int accessMode) throws ODMGException {						try {							new Configuration().configure();						}						catch (HibernateException he) {							throw new ODMGException( he.getMessage() );						}						sessionFactory = (SessionFactory) SessionFactoryObjectFactory.getNamedInstance(name);						if (sessionFactory==null) throw new ODMGException("No SessionFactory was associated with the given JDNI name");						/*try {							sessionFactory = (SessionFactory) NamingHelper.getInitialContext( Environment.getProperties() ).lookup(name);						}						catch (NamingException ne) {							throw new ODMGException( ne.getMessage() );						}*/					}										/**					 * Close the Database (but not the underlying SessionFactory).					 * @see org.odmg.Database#close()					 */					public void close() throws ODMGException {						//TODO: Wait for all transactions to complete?						//if ( !threadTransactions.isEmpty() ) throw new ODMGException("Running transactions");						sessionFactory = null;						//TODO: remove it from ThreadLocal storage on Implementation					}										/**					 * Bind a name to a persistent object, making the object persistent if necessary.					 * @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						}						catch (HibernateException he) {							throw new ODMGRuntimeException( he.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 {								nameObj = (Name) s.load(Name.class, name);							}							catch (ObjectNotFoundException onfe) {								throw new ObjectNameNotFoundException();							}							return s.load( nameObj.getPersistentClass(), nameObj.getId() );						}						catch (HibernateException he) {							throw new ODMGRuntimeException( he.getMessage() );						}					}										/**					 * Unbind the given name.					 * @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() );						}					}										/**					 * Make the given object persistent.					 * @see org.odmg.Database#makePersistent(Object)					 */					public void makePersistent(Object object) {						try {							getSession().save(object);						}						catch (HibernateException he) {							throw new ODMGRuntimeException( he.getMessage() );						}					}					/**					 * Delete the given object from the database.					 * @see org.odmg.Database#deletePersistent(Object)					 */					public void deletePersistent(Object object) {						try {							getSession().delete(object);						}						catch (HibernateException he) {							throw new ODMGRuntimeException( he.getMessage() );						}					}									}																															

相关资源