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 import java.io.File;
9
10 /***
11 * @author Stephane JAIS
12 */
13 public class HypersonicDataSource
14 extends HibernateDataSource
15 {
16 private static final Logger logger = Logger.getLogger(HypersonicDataSource.class);
17 private static SessionFactory sessionFactory ;
18 public static final ThreadLocal session = new ThreadLocal();
19
20 private String itsPath;
21 private boolean itsCreate;
22
23 public HypersonicDataSource(String itsPath, boolean itsCreate)
24 {
25 this.itsPath = itsPath;
26 this.itsCreate=itsCreate;
27 }
28 public ThreadLocal getThreadLocalSession()
29 {
30 return session;
31 }
32
33 public SessionFactory getSessionFactory()
34 {
35 if (sessionFactory != null)
36 return sessionFactory;
37
38 logger.debug("Creating hsql session factory. path="+itsPath);
39 try
40 {
41 Configuration config = new Configuration()
42 .configure();
43 config.getProperties().clear();
44 config.setProperty("hibernate.connection.driver_class","org.hsqldb.jdbcDriver")
45 .setProperty("hibernate.dialect","net.sf.hibernate.dialect.HSQLDialect")
46 .setProperty("hibernate.connection.url","jdbc:hsqldb:"+itsPath)
47 .setProperty("hibernate.connection.username","sa")
48 .setProperty("hibernate.connection.password","");
49 config.setProperty("hibernate.hbm2ddl.auto",itsCreate ? "create" : "");
50 sessionFactory = config.buildSessionFactory();
51
52 } catch (HibernateException e)
53 {
54 logger.error("Could not configure hsql.", e);
55 }
56 return sessionFactory;
57 }
58
59 public void closeSessionFactory()
60 throws Exception
61 {
62 if (sessionFactory != null)
63 {
64 logger.debug("Closing hibernate session factory.");
65 sessionFactory.close();
66 sessionFactory = null;
67 }
68 }
69 }