1 package net.sf.cantina.datasource;
2
3 import net.sf.hibernate.SessionFactory;
4 import net.sf.hibernate.HibernateException;
5 import net.sf.hibernate.cfg.Configuration;
6 import org.apache.log4j.Logger;
7
8 /***
9 * @author Stephane JAIS
10 */
11 public class UserDefinedDataSource
12 extends HibernateDataSource
13 {
14 public static Logger logger = Logger.getLogger(UserDefinedDataSource.class);
15 private static SessionFactory sessionFactory;
16 public static final ThreadLocal session = new ThreadLocal();
17
18 public SessionFactory getSessionFactory()
19 {
20 if (sessionFactory != null)
21 return sessionFactory;
22
23 logger.debug("Creating session factory.");
24 try
25 {
26 SessionFactory newSessionFactory = new Configuration()
27 .configure()
28 .buildSessionFactory();
29 sessionFactory = newSessionFactory;
30 } catch (HibernateException e)
31 {
32 logger.error("Could not configure hibernate.", e);
33 }
34 return sessionFactory;
35 }
36
37 public void closeSessionFactory()
38 throws Exception
39 {
40 if (sessionFactory != null)
41 {
42 logger.debug("Closing hibernate session factory.");
43 sessionFactory.close();
44 sessionFactory = null;
45 }
46 }
47
48 protected ThreadLocal getThreadLocalSession()
49 {
50 return session;
51 }
52
53 }