//获取session实例
public session getsession(){
servletcontext contxt = ....
sessionfactory sessions = ....
datasource ds = (datasource)context.getattribute(globals.data_source_key);
final connection conn = ds.getconnection();
return sessions.opensession(conn);
}
//释放session
public void closesession(session ssn){
ssn.connection().close();
ssn.close();
}
<!-- connection pool settings -->
<property name="connection.provider_class">
com.liusoft.dlog4j.db.datasourceconnprovider</property>
<property name="dscp.datasource">org.apache.commons.dbcp.basicdatasource</property>
<property name="dscp.driverclassname">sun.jdbc.odbc.jdbcodbcdriver</property>
<property name="dscp.url">jdbc:odbc:dlog4j</property>
<property name="dscp.username">admin</property>
<property name="dscp.password"></property>
<property name="dscp.initialsize">1</property>
<property name="dscp.maxactive">200</property>
<property name="dscp.maxwait">2000</property>
<property name="dscp.defaultautocommit">false</property>
<property name="dscp.defaultreadonly">false</property>
<property name="dscp.removeabandoned">true</property>
<property name="dscp.removeabandonedtimeout">120</property>
<!--
<property name="dscp.defaulttransactionisolation">1</property>
-->
<property name="dscp.poolpreparedstatements">true</property>
<property name="dscp.maxopenpreparedstatements">1000</property>
package com.liusoft.dlog4j.db;
import java.lang.reflect.method;
import java.sql.connection;
import java.sql.sqlexception;
import java.util.iterator;
import java.util.properties;
import javax.sql.datasource;
import org.apache.commons.beanutils.beanutils;
import org.hibernate.hibernateexception;
import org.hibernate.connection.connectionprovider;
import com.liusoft.dlog4j.globals;
import com.liusoft.dlog4j.util.stringutils;
/**
* 让hibernate支持各种数据源
* @author winter lau
*/
public class datasourceconnprovider implements connectionprovider {
private final static string base_key = "dscp.";
private final static string encoding_key = "dscp.encoding";
private final static string datasource_key = "dscp.datasource";
protected datasource datasource;
/* (non-javadoc)
* @see org.hibernate.connection.connectionprovider#configure(java.util.properties)
*/
public void configure(properties props) throws hibernateexception {
string datasourceclass = null;
properties new_props = new properties();
iterator keys = props.keyset().iterator();
while(keys.hasnext()){
string key = (string)keys.next();
if(datasource_key.equalsignorecase(key)){
datasourceclass = props.getproperty(key);
}
else if(key.startswith(base_key)){
string value = props.getproperty(key);
value = stringutils.replace(value, "{dlog4j}", globals.webapp_path);
new_props.setproperty(key.substring(base_key.length()), value);
}
}
if(datasourceclass == null)
throw new hibernateexception("property 'dscp.datasource' no defined.");
try {
datasource = (datasource)class.forname(datasourceclass).newinstance();
beanutils.populate(datasource, new_props);
} catch (exception e) {
throw new hibernateexception(e);
}
}
/* (non-javadoc)
* @see org.hibernate.connection.connectionprovider#getconnection()
*/
public connection getconnection() throws sqlexception {
final connection conn = datasource.getconnection();
if(useproxy && conn!=null){
return (new _connection(conn,encoding)).getconnection();
}
return conn;
}
/* (non-javadoc)
* @see org.hibernate.connection.connectionprovider#closeconnection(java.sql.connection)
*/
public void closeconnection(connection conn) throws sqlexception {
if(conn!=null && !conn.isclosed())
conn.close();
}
/* (non-javadoc)
* @see org.hibernate.connection.connectionprovider#close()
*/
public void close() throws hibernateexception {
if(datasource != null)
try {
method mclose = datasource.getclass().getmethod("close",null);
mclose.invoke(datasource, null);
} catch (exception e) {
throw new hibernateexception(e);
}
datasource = null;
}
/* (non-javadoc)
* @see org.hibernate.connection.connectionprovider#supportsaggressiverelease()
*/
public boolean supportsaggressiverelease() {
return false;
}
}
新闻热点
疑难解答