Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
automan committed Jun 8, 2012
1 parent 5878fe2 commit 5eabd4f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 55 deletions.
115 changes: 66 additions & 49 deletions src/main/java/com/taobao/fario/server/db/HibernateSessionFactory.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,57 @@
package com.taobao.fario.server.db;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.w3c.dom.Document;

public class HibernateSessionFactory {

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static String CONFIG_FILE_LOCATION = "src/main/java/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;

private static String user_configFile = "src/main/java/com/taobao/fario/server/service/User.hbm.xml";
private static String shopinfo_configFile = "src/main/java/com/taobao/fario/server/service/ShopInfo.hbm.xml";

static {
try {
configuration.configure(configFile);
try {
configuration.configure(new File(configFile));
DocumentBuilder documentBuilder = DocumentBuilderFactory
.newInstance().newDocumentBuilder();

Document doc = documentBuilder.parse(new File(user_configFile));
configuration.addDocument(doc);

Document docshop = documentBuilder.parse(new File(shopinfo_configFile));
configuration.addDocument(docshop);

sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}

}

private HibernateSessionFactory() {
}

/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
Expand All @@ -44,60 +62,59 @@ public static Session getSession() throws HibernateException {
threadLocal.set(session);
}

return session;
}
return session;
}

/**
* Rebuild hibernate session factory
*
*/
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

/**
* return session factory
*
*/
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}

/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}

/**
* return hibernate configuration
*
*/
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@

<session-factory>

<!--

<property name="connection.url">jdbc:postgresql://ec2-23-21-132-139.compute-1.amazonaws.com/dkruvhrubx</property>
<property name="connection.username">dkruvhrubx</property>
<property name="connection.password">vSbRdgipqkNF4rWzuny6</property>
-->

<!--
<property name="connection.url">jdbc:postgresql://localhost/fario</property>
<property name="connection.username">postgres</property>
<property name="connection.password">Taobao2011</property>

-->

<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="show_sql">false</property>

<mapping resource="com/taobao/fario/server/service/User.hbm.xml" />
<mapping resource="com/taobao/fario/server/service/ShopInfo.hbm.xml" />



</session-factory>

Expand Down

0 comments on commit 5eabd4f

Please sign in to comment.