Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/db
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25164
Modified Files:
HibernateUtil.java
Log Message:
Added setMode() - dynamic configuration (HSQLDB/PostgreSQL)
Index: HibernateUtil.java
===================================================================
RCS file: /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/db/HibernateUtil.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** HibernateUtil.java 28 Jul 2005 06:50:19 -0000 1.1
--- HibernateUtil.java 1 Aug 2005 10:33:37 -0000 1.2
***************
*** 7,10 ****
--- 7,16 ----
package net.sourceforge.bprocessor.model.db;
+ import net.sourceforge.bprocessor.model.Attribute;
+ import net.sourceforge.bprocessor.model.Edge;
+ import net.sourceforge.bprocessor.model.Vertex;
+ import net.sourceforge.bprocessor.model.Domain;
+ import net.sourceforge.bprocessor.model.Surface;
+
import org.hibernate.Session;
import org.hibernate.SessionFactory;
***************
*** 29,41 ****
private static final ThreadLocal TL = new ThreadLocal();
/**
* Constructor
*/
private HibernateUtil() {
! try {
! sf = new Configuration().configure().buildSessionFactory();
! } catch (Exception e) {
! log.fatal("Exception", e);
! }
}
--- 35,50 ----
private static final ThreadLocal TL = new ThreadLocal();
+ /** MODE_HSQLDB */
+ public static final int MODE_HSQLDB = 0;
+
+ /** MODE_POSTGRESQL */
+ public static final int MODE_POSTGRESQL = 1;
+
/**
* Constructor
*/
private HibernateUtil() {
! sf = null;
! setMode(MODE_HSQLDB, true, "jdbc:hsqldb:mem:bprocessor", "", "");
}
***************
*** 52,55 ****
--- 61,131 ----
/**
+ * Set the mode of the persistence layer
+ * @param mode The mode
+ * @param autodrop Should the database be dropped ?
+ * @param url The connection url
+ * @param user The user name
+ * @param password The password
+ */
+ public synchronized void setMode(int mode, boolean autodrop,
+ String url, String user, String password) {
+ try {
+ if (sf != null && !sf.isClosed()) {
+ sf.close();
+ }
+
+ Configuration cfg = new Configuration();
+
+ if (mode == MODE_HSQLDB) {
+ cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
+ cfg.setProperty("hibernate.use_outer_join", "false");
+ cfg.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
+ } else if (mode == MODE_POSTGRESQL) {
+ cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
+ cfg.setProperty("hibernate.use_outer_join", "true");
+ cfg.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
+ } else {
+ log.error("Unsupported database: " + mode);
+ log.warn("Defaulting to HSQLDB/Memory");
+ url = "jdbc:hsqldb:mem:bprocessor";
+ user = null;
+ password = null;
+ autodrop = true;
+ cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
+ cfg.setProperty("hibernate.use_outer_join", "false");
+ cfg.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
+ }
+
+ if (autodrop) {
+ cfg.setProperty("hibernate.hbm2ddl.auto", "create-drop");
+ } else {
+ cfg.setProperty("hibernate.hbm2ddl.auto", "update");
+ }
+
+ cfg.setProperty("hibernate.show_sql", "false");
+ cfg.setProperty("hibernate.order_updates", "true");
+
+ cfg.setProperty("hibernate.connection.url", url);
+
+ if (user != null && !user.trim().equals("")) {
+ cfg.setProperty("hibernate.connection.username", user);
+ }
+ if (password != null && !password.trim().equals("")) {
+ cfg.setProperty("hibernate.connection.password", password);
+ }
+
+ cfg.addClass(Attribute.class);
+ cfg.addClass(Edge.class);
+ cfg.addClass(Vertex.class);
+ cfg.addClass(Domain.class);
+ cfg.addClass(Surface.class);
+
+ sf = cfg.buildSessionFactory();
+ } catch (Exception e) {
+ log.fatal("Exception", e);
+ }
+ }
+
+ /**
* Get the current session
* @return The session
|