1 22 package org.jboss.test.hibernate.timers; 23 24 import java.util.List ; 25 import javax.naming.InitialContext ; 26 27 import org.jboss.logging.Logger; 28 import org.hibernate.SessionFactory; 29 import org.hibernate.LockMode; 30 import org.hibernate.HibernateException; 31 32 36 public class TimersFactory 37 { 38 private static final Logger log = Logger.getLogger(Timers.class); 39 40 private final SessionFactory sessionFactory = getSessionFactory(); 41 42 protected SessionFactory getSessionFactory() 43 { 44 try 45 { 46 InitialContext ctx = new InitialContext (); 47 SessionFactory factory = (SessionFactory) ctx.lookup("java:/hib-timers/SessionFactory"); 48 ctx.close(); 49 return factory; 50 } 51 catch (Exception e) 52 { 53 log.error("Could not locate SessionFactory in JNDI", e); 54 throw new IllegalStateException ("Could not locate SessionFactory in JNDI"); 55 } 56 } 57 58 public void persist(Timers transientInstance) 59 { 60 log.debug("persisting Timers instance"); 61 try 62 { 63 sessionFactory.getCurrentSession().persist(transientInstance); 64 log.debug("persist successful"); 65 } 66 catch (RuntimeException re) 67 { 68 log.error("persist failed", re); 69 throw re; 70 } 71 } 72 73 public void attachDirty(Timers instance) 74 { 75 log.debug("attaching dirty Timers instance"); 76 try 77 { 78 sessionFactory.getCurrentSession().saveOrUpdate(instance); 79 log.debug("attach successful"); 80 } 81 catch (RuntimeException re) 82 { 83 log.error("attach failed", re); 84 throw re; 85 } 86 } 87 88 public void attachClean(Timers instance) 89 { 90 log.debug("attaching clean Timers instance"); 91 try 92 { 93 sessionFactory.getCurrentSession().lock(instance, LockMode.NONE); 94 log.debug("attach successful"); 95 } 96 catch (RuntimeException re) 97 { 98 log.error("attach failed", re); 99 throw re; 100 } 101 } 102 103 public void delete(Timers persistentInstance) 104 { 105 log.debug("deleting Timers instance"); 106 try 107 { 108 sessionFactory.getCurrentSession().delete(persistentInstance); 109 log.debug("delete successful"); 110 } 111 catch (RuntimeException re) 112 { 113 log.error("delete failed", re); 114 throw re; 115 } 116 } 117 118 public Timers merge(Timers detachedInstance) 119 { 120 log.debug("merging Timers instance"); 121 try 122 { 123 Timers result = (Timers) sessionFactory.getCurrentSession() 124 .merge(detachedInstance); 125 log.debug("merge successful"); 126 return result; 127 } 128 catch (RuntimeException re) 129 { 130 log.error("merge failed", re); 131 throw re; 132 } 133 } 134 135 public List listUsers() throws HibernateException 136 { 137 return sessionFactory.getCurrentSession() 138 .createQuery("from Timers") 139 .list(); 140 } 141 142 public Timers findById(TimersID id) 143 { 144 log.debug("getting Timers instance with id: " + id); 145 try 146 { 147 Timers instance = (Timers) sessionFactory.getCurrentSession() 148 .get("org.jboss.ejb.txtimer.data.Timers", id); 149 if (instance == null) 150 { 151 log.debug("get successful, no instance found"); 152 } 153 else 154 { 155 log.debug("get successful, instance found"); 156 } 157 return instance; 158 } 159 catch (RuntimeException re) 160 { 161 throw re; 162 } 163 } 164 165 } | Popular Tags |