?? persistenceutils.java
字號:
package cn.myapps.base.dao;
import java.beans.PropertyVetoException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* The persistence utility.
*/
public class PersistenceUtils {
private static Log logger = LogFactory.getLog(PersistenceUtils.class);
private static final ThreadLocal sessionSignal = new ThreadLocal();
private static Log log = LogFactory.getLog(PersistenceUtils.class);
private static DataSource ds = null; // DBCP DataSource
public static SessionSignal getSessionSignal() {
SessionSignal sg = (SessionSignal) sessionSignal.get();
if (sg == null) {
sg = new SessionSignal();
sessionSignal.set(sg);
}
return sg;
}
/**
* Get the current session.
*
* @return The current session.
* @throws Exception
*/
public static Session currentSession() throws Exception {
try {
logger.debug("get session!");
return HibernateBaseDAO.currentSession();
} catch (HibernateException he) {
throw new Exception(he);
}
}
/**
* Close the session.
*
* @throws Exception
*/
public static void closeSession() throws Exception {
try {
logger.debug("close session!");
HibernateBaseDAO.closeSession();
} catch (HibernateException he) {
throw new Exception(he);
}
}
/**
* Open the transcation.
*
* @throws Exception
*/
public static void beginTransaction() throws Exception {
try {
HibernateBaseDAO.beginTransaction();
} catch (HibernateException he) {
throw new Exception(he);
}
}
/**
* Commit the transcation.
*
* @throws Exception
*/
public static void commitTransaction() throws Exception {
try {
HibernateBaseDAO.commitTransaction();
} catch (HibernateException he) {
throw new Exception(he);
}
}
/**
* Roll back the transaction.
*
* @throws Exception
*/
public static void rollbackTransaction() throws Exception {
try {
HibernateBaseDAO.rollbackTransaction();
} catch (HibernateException he) {
throw new Exception(he);
}
}
public static DataSource getDataSource() throws Exception {
/*if (ds == null) {*/
Configuration cfg = new Configuration().configure();
Properties prop = cfg.getProperties();
String username = prop.getProperty("hibernate.connection.username");
String password = prop.getProperty("hibernate.connection.password");
String driver = prop
.getProperty("hibernate.connection.driver_class");
String url = prop.getProperty("hibernate.connection.url");
ds = getDBCPDataSource(username, password, driver, url);
// }
return ds;
}
public static DataSource getC3P0DataSource(String username,
String password, String driver, String url) throws Exception {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setUser(username);
ds.setPassword(password);
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setAutoCommitOnClose(false);
ds.setMaxPoolSize(20);
ds.setMaxIdleTime(10);
ds.setMaxStatements(5);
return ds;
}
public static DataSource getDBCPDataSource(String username,
String password, String driver, String url) {
BasicDataSource ds = new BasicDataSource();
ds.setUsername(username);
ds.setPassword(password);
ds.setDriverClassName(driver);
ds.setUrl(url);
// ds.setPoolPreparedStatements(true);
// ds.setMaxOpenPreparedStatements(10);
//ds.setInitialSize(10);
ds.setMaxIdle(1);
ds.setDefaultAutoCommit(false);
ds.setMaxActive(10);
return ds;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -