//获取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; } } |