1 26 28 package de.nava.informa.impl.hibernate; 29 30 import java.sql.Connection ; 31 import java.util.Properties ; 32 33 import net.sf.hibernate.HibernateException; 34 import net.sf.hibernate.Session; 35 import net.sf.hibernate.SessionFactory; 36 import net.sf.hibernate.cfg.Configuration; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 47 public class SessionHandler { 48 49 private static Log logger = LogFactory.getLog(SessionHandler.class); 50 51 private static SessionHandler myInstance; 52 53 private Configuration cfg; 54 private SessionFactory sessFactory; 55 private Connection conn; 56 private Session curSession; 57 58 73 private SessionHandler(Properties props) throws HibernateException { 74 cfg = new Configuration(); 76 77 try { 79 cfg.configure(); 80 } catch (HibernateException he) { 81 logger.info("Can't find \"hibernate.cfg.xml\" in classpath."); 82 } 83 84 cfg 86 .addClass(Channel.class) 87 .addClass(Item.class) 88 .addClass(ItemGuid.class) 89 .addClass(ItemEnclosure.class) 90 .addClass(ItemSource.class) 91 .addClass(Cloud.class) 92 .addClass(Category.class) 93 .addClass(ChannelGroup.class) 94 .addClass(ChannelSubscription.class) 95 .addClass(Image.class) 96 .addClass(ItemMetadata.class) 97 .addClass(TextInput.class); 98 99 if (props != null) 101 cfg.addProperties(props); 102 103 sessFactory = cfg.buildSessionFactory(); 105 } 106 107 114 public static SessionHandler getInstance(Properties props) throws HibernateException { 115 if (myInstance == null) { 116 myInstance = new SessionHandler(props); 117 } 118 return myInstance; 119 } 120 121 125 public static SessionHandler getInstance() throws HibernateException { 126 return getInstance(null); 127 } 128 129 133 public Session getSession() throws HibernateException { 134 if (conn != null) 135 curSession = sessFactory.openSession(conn); 136 else 137 curSession = sessFactory.openSession(); 138 return curSession; 139 } 140 141 146 public Session getSession(Connection conn) { 147 this.conn = conn; 148 curSession = sessFactory.openSession(conn); 149 return curSession; 150 } 151 152 155 public Connection getConnection() { 156 return conn; 157 } 158 159 162 public void setConnection(Connection connection) { 163 conn = connection; 164 } 165 166 169 public boolean isSessionOpen() 170 { 171 return curSession.isOpen(); 172 } 173 174 } 175 | Popular Tags |