373 lines
11 KiB
Plaintext
373 lines
11 KiB
Plaintext
package th.co.muangthai.endrprint.model.hibernate;
|
|
|
|
import org.apache.commons.dbcp.BasicDataSource;
|
|
import org.apache.log4j.Logger;
|
|
import org.hibernate.HibernateException;
|
|
import org.hibernate.Query;
|
|
import org.hibernate.Session;
|
|
import org.hibernate.SessionFactory;
|
|
import org.hibernate.cfg.Configuration;
|
|
import org.hibernate.service.ServiceRegistry;
|
|
import org.hibernate.service.ServiceRegistryBuilder;
|
|
import th.co.muangthai.endrprint.util.SessionUtil;
|
|
import th.co.muangthai.endrprint.util.TextContent;
|
|
|
|
import javax.naming.Context;
|
|
import javax.naming.InitialContext;
|
|
import javax.sql.DataSource;
|
|
import java.math.BigDecimal;
|
|
import java.sql.CallableStatement;
|
|
import java.sql.Connection;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Properties;
|
|
|
|
//import th.co.muangthai.util.VSMUtil;
|
|
|
|
public class HibernateUtil {
|
|
|
|
private static ServiceRegistry serviceRegistry;
|
|
private static SessionFactory sessionFactory;
|
|
public static DataSource ds;
|
|
private static Logger log = Logger.getLogger(HibernateUtil.class);
|
|
private static boolean dataSourceFlag = false;
|
|
|
|
private static boolean monitorConnectonFlag = false;
|
|
|
|
|
|
static {
|
|
try {
|
|
//System.out.println( javax.persistence.OneToMany.class.toString() + " loaded into a classloader" + javax.persistence.OneToMany.class.getClassLoader().toString() );
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
//System.err.println("Initial SessionFactory creation failed." + ex);
|
|
throw new ExceptionInInitializerError(ex);
|
|
}
|
|
}
|
|
|
|
private static Properties hibernateProperties;
|
|
|
|
private static void createFactory() {
|
|
Configuration configuration = new Configuration();
|
|
configuration.configure();
|
|
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
|
|
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
|
|
|
BasicDataSource bds = new BasicDataSource();
|
|
hibernateProperties = configuration.getProperties();
|
|
|
|
if ( null == configuration.getProperty("connection.datasource") ) {
|
|
dataSourceFlag = false;
|
|
bds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
|
|
bds.setUsername(configuration.getProperty("connection.username"));
|
|
bds.setPassword(configuration.getProperty("connection.password"));
|
|
bds.setUrl(configuration.getProperty("connection.url"));
|
|
// bds.setMaxActive(20);
|
|
// bds.setMaxIdle(2);
|
|
// bds.setMaxWait(30 * 1000);
|
|
//TextContent
|
|
|
|
// if(bds.getUrl().toUpperCase().contains("UAT")){
|
|
// SessionUtil.getLofSession().setAttribute(TextContent.sessionServerTxt,"UAT");
|
|
// }else{
|
|
// SessionUtil.getLofSession().setAttribute(TextContent.sessionServerTxt,"Production");
|
|
// }
|
|
|
|
ds = (DataSource) bds;
|
|
|
|
} else {
|
|
try {
|
|
dataSourceFlag = true;
|
|
Context ctx = new InitialContext();
|
|
|
|
// if(configuration.getProperty("connection.datasource").toUpperCase().contains("UAT")){
|
|
// SessionUtil.getLofSession().setAttribute(TextContent.sessionServerTxt,"UAT");
|
|
// }else{
|
|
// SessionUtil.getLofSession().setAttribute(TextContent.sessionServerTxt,"Production");
|
|
// }
|
|
|
|
ds = (DataSource) ctx.lookup(configuration.getProperty("connection.datasource"));
|
|
} catch (Exception e) {
|
|
log.error(e.toString(), e);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private static int countOpenConnection = 0;
|
|
private static int countCloseConnection = 0;
|
|
|
|
public static Connection getConnection() throws SQLException {
|
|
// Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@10.1.0.187:1521:orcl", "mtram", "passw0rd");
|
|
// return conn;
|
|
//log.info("getConnection #");
|
|
Connection conn = ds.getConnection();
|
|
|
|
// Debug
|
|
if (monitorConnectonFlag)
|
|
{
|
|
countOpenConnection++;
|
|
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
|
|
log.info("\nopen connection : " + stack[2].getClassName() + " : " + stack[2].getMethodName());
|
|
log.info("connection #" + countOpenConnection);
|
|
}
|
|
// End Debug
|
|
|
|
|
|
|
|
return conn;
|
|
}
|
|
|
|
|
|
public static void closeConnection(Connection conn) throws SQLException {
|
|
if (conn != null && !conn.isClosed())
|
|
{
|
|
|
|
|
|
conn.close();
|
|
if (monitorConnectonFlag)
|
|
{
|
|
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
|
|
log.info("\nclose connection : " + stack[2].getClassName() + " : " + stack[2].getMethodName());
|
|
countCloseConnection++;
|
|
log.info("closed connection #" + countCloseConnection);
|
|
// Debug
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//return ds.getConnection();
|
|
}
|
|
|
|
|
|
public static void close(ResultSet rs,CallableStatement cstm) throws SQLException {
|
|
if(null != rs){
|
|
try {
|
|
rs.close();
|
|
}catch (Exception e){
|
|
log.error(e.toString(),e);
|
|
}
|
|
}
|
|
|
|
if(null != cstm){
|
|
try {
|
|
cstm.close();
|
|
}catch (Exception e){
|
|
log.error(e.toString(),e);
|
|
}
|
|
}
|
|
}
|
|
//Connection = DriverManager.getConnection("jdbc:oracle:thin:@10.1.0.187:1521:orcl", "mtram", "passw0rd");
|
|
|
|
public static SessionFactory getSessionFactory() {
|
|
return sessionFactory;
|
|
}
|
|
|
|
private static int countOpenSession = 0;
|
|
public static Session getCurrentSession() {
|
|
// log.info("getCurrentSession s #" + countOpenSession);
|
|
if (sessionFactory == null || sessionFactory.isClosed()) {
|
|
createFactory();
|
|
}
|
|
|
|
Session session = null;
|
|
try {
|
|
|
|
session = sessionFactory.openSession();
|
|
|
|
if (monitorConnectonFlag)
|
|
{
|
|
countOpenSession++;
|
|
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
|
|
//log.info("\nopen : " + stack[2].getClassName() + " : " + stack[2].getMethodName());
|
|
//log.info("session #" + countOpenSession);
|
|
}
|
|
|
|
// SessionBuilder sb = sessionFactory.withOptions();
|
|
// Session session = sb.connection(ds.getConnection()).openSession();
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
log.error(e.toString(), e);
|
|
}
|
|
// log.info("getCurrentSession e #" + countOpenSession);
|
|
return session;
|
|
|
|
// Session sess = null;
|
|
// try {
|
|
// sess = sessionFactory.getCurrentSession();
|
|
// } catch (org.hibernate.HibernateException he) {
|
|
//
|
|
// return sessionFactory.openSession();
|
|
// }
|
|
// return sess;
|
|
|
|
|
|
}
|
|
|
|
private static int countCloseSession = 0;
|
|
|
|
public static void closeSession(Session session) {
|
|
if (session.isOpen()) {
|
|
|
|
|
|
session.close();
|
|
if (monitorConnectonFlag)
|
|
{
|
|
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
|
|
//log.info("\nclose : " + stack[2].getClassName() + " : " + stack[2].getMethodName());
|
|
countCloseSession++;
|
|
//log.info("closed session #" + countCloseSession);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static Object load(Object object, Integer id) {
|
|
|
|
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
|
|
try
|
|
{
|
|
session.beginTransaction();
|
|
Object loadedObject = (Object) session.load(object.getClass(), id);
|
|
session.getTransaction().commit();
|
|
return loadedObject;
|
|
}
|
|
finally {
|
|
HibernateUtil.closeSession(session);
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
public static Object save(Object object) {
|
|
Session session = HibernateUtil.getCurrentSession();
|
|
try
|
|
{
|
|
session.beginTransaction();
|
|
session.save(object);
|
|
session.getTransaction().commit();
|
|
}
|
|
finally {
|
|
HibernateUtil.closeSession(session);
|
|
}
|
|
return object;
|
|
}
|
|
|
|
public static Object update(Object object) {
|
|
Session session = HibernateUtil.getCurrentSession();
|
|
try
|
|
{
|
|
session.beginTransaction();
|
|
session.update(object);
|
|
session.getTransaction().commit();
|
|
}
|
|
finally {
|
|
HibernateUtil.closeSession(session);
|
|
}
|
|
return object;
|
|
}
|
|
|
|
public static Object delete(Object object, Long id) {
|
|
|
|
Session session = HibernateUtil.getCurrentSession();
|
|
try
|
|
{
|
|
session.beginTransaction();
|
|
Object loadedObject = (Object) session.load(object.getClass(), id);
|
|
if (null != loadedObject) {
|
|
session.delete(loadedObject);
|
|
}
|
|
session.getTransaction().commit();
|
|
}
|
|
finally {
|
|
HibernateUtil.closeSession(session);
|
|
}
|
|
|
|
return object;
|
|
}
|
|
|
|
public static List list(Class aClass) {
|
|
|
|
Session session = HibernateUtil.getCurrentSession();
|
|
session.beginTransaction();
|
|
List objectList = null;
|
|
try {
|
|
|
|
objectList = session.createQuery("from " + aClass.getName()).list();
|
|
session.getTransaction().commit();
|
|
|
|
} catch (HibernateException e) {
|
|
e.printStackTrace();
|
|
session.getTransaction().rollback();
|
|
}
|
|
finally {
|
|
HibernateUtil.closeSession(session);
|
|
}
|
|
|
|
return objectList;
|
|
}
|
|
|
|
|
|
|
|
public static BigDecimal getSequence() {
|
|
|
|
List list = new ArrayList();
|
|
BigDecimal seq = null;
|
|
Session session = HibernateUtil.getCurrentSession();
|
|
try {
|
|
|
|
Query query = session.createSQLQuery(" select GROUP_SEQ.nextval from dual ");
|
|
list = query.list();
|
|
} catch (Exception ex) {
|
|
log.error(ex.toString(), ex);
|
|
}
|
|
finally {
|
|
HibernateUtil.closeSession(session);
|
|
}
|
|
|
|
if (list.size() > 0) {
|
|
seq = new BigDecimal(list.get(0).toString());
|
|
}
|
|
return seq;
|
|
}
|
|
|
|
public static BigDecimal getSequenceInputName(String sequenceName) {
|
|
|
|
List list = new ArrayList();
|
|
BigDecimal seq = null;
|
|
Session session = HibernateUtil.getCurrentSession();
|
|
try {
|
|
|
|
Query query = session.createSQLQuery(" select "+sequenceName+".nextval from dual ");
|
|
list = query.list();
|
|
} catch (Exception ex) {
|
|
log.error(ex.toString(), ex);
|
|
}
|
|
finally {
|
|
HibernateUtil.closeSession(session);
|
|
}
|
|
|
|
if (list.size() > 0) {
|
|
seq = new BigDecimal(list.get(0).toString());
|
|
}
|
|
return seq;
|
|
}
|
|
|
|
public static Properties getHibernateProperties() {
|
|
return hibernateProperties;
|
|
}
|
|
|
|
|
|
}
|