<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    seaairland

     

    Hibernate源碼分析---數(shù)據(jù)庫連接池

    1:net.sf.hibernate.connection
    此包中的類為提供數(shù)據(jù)源連接池的類,其中用到了C3P0,DBCP,DriverManager等第三方連接池插件;
    其中ConnectionProvider為一致對(duì)外接口;所有的功能類都實(shí)現(xiàn)的他的接口;
    /**
    ???????? * Initialize the connection provider from given properties.
    ???????? * @param props <tt>SessionFactory</tt> properties
    ???????? */
    public void configure(Properties props) throws HibernateException;
    ????????/**
    ???????? * Grab a connection
    ???????? * @return a JDBC connection
    ???????? * @throws SQLException
    ???????? */
    ????????public Connection getConnection() throws SQLException;
    ????????/**
    ???????? * Dispose of a used connection.
    ???????? * @param conn a JDBC connection
    ???????? * @throws SQLException
    ???????? */
    ????????public void closeConnection(Connection conn) throws SQLException;
    ????????
    ????????/**
    ???????? * Release all resources held by this provider. JavaDoc requires a second sentence.
    ???????? * @throws HibernateException
    ???????? */
    ????????public void close() throws HibernateException;


    源碼中的注釋把方法功能描述的很具體,我再次就不再加介紹了,現(xiàn)在具體的拿出兩個(gè)實(shí)現(xiàn)類來具體看一下他的功能;
    1.1????????C3P0ConnectionProvider implements ConnectionProvider
    public void configure(Properties props) throws HibernateException {
    //獲取數(shù)據(jù)庫的驅(qū)程和URL;
    ????????????????String jdbcDriverClass = props.getProperty(Environment.DRIVER);
    ????????????????String jdbcUrl = props.getProperty(Environment.URL);
    ????????//對(duì)第三方插件所需要的屬性進(jìn)行定制包裝;
    ????????????????Properties connectionProps = ConnectionProviderFactory.getConnectionProperties(props);
    ????????????????
    ????????????????log.info("C3P0 using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl);
    ????????????????log.info("Connection properties: " + connectionProps);
    ????????????????//加載數(shù)據(jù)庫驅(qū)動(dòng)程序
    ????????????????if (jdbcDriverClass==null) {
    ????????????????????????log.warn("No JDBC Driver class was specified by property " + Environment.DRIVER);
    ????????????????}
    ????????????????else {
    ????????????????????????try {
    ????????????????????????????????Class.forName(jdbcDriverClass);
    ????????????????????????}
    ????????????????????????catch (ClassNotFoundException cnfe) {
    ????????????????????????????????String msg = "JDBC Driver class not found: " + jdbcDriverClass;
    ????????????????????????????????log.fatal(msg);
    ????????????????????????????????throw new HibernateException(msg);
    ????????????????????????}
    ????????????????}
    ????????????????
    ????????????????try {
    ????????//加載配置文件中的properties;????????????????
    ????????int minPoolSize = PropertiesHelper.getInt(Environment.C3P0_MIN_SIZE, props, 1);
    ????????int maxPoolSize = PropertiesHelper.getInt(Environment.C3P0_MAX_SIZE, props, 100);
    ????????int maxIdleTime = PropertiesHelper.getInt(Environment.C3P0_TIMEOUT, props, 0);
    ????????int maxStatements = PropertiesHelper.getInt(Environment.C3P0_MAX_STATEMENTS, props, 0);
    ????????int acquireIncrement = PropertiesHelper.getInt(Environment.C3P0_ACQUIRE_INCREMENT, props, 1);
    ????????int idleTestPeriod = PropertiesHelper.getInt(Environment.C3P0_IDLE_TEST_PERIOD, props, 0);
    ????????boolean validateConnection = PropertiesHelper.getBoolean(Environment.C3P0_VALIDATE_CONNECTION, props);
    ????????????????????????//有C3P0的連接池接口來設(shè)置第三方插件所需要的屬性
    ????????????此處為與第三方插件的結(jié)合點(diǎn);
    ????????????????????????PoolConfig pcfg = new PoolConfig();
    ????????????????????????pcfg.setInitialPoolSize(minPoolSize);
    ????????????????????????pcfg.setMinPoolSize(minPoolSize);
    ????????????????????????pcfg.setMaxPoolSize(maxPoolSize);
    ????????????????????????pcfg.setAcquireIncrement(acquireIncrement);
    ????????????????????????pcfg.setMaxIdleTime(maxIdleTime);
    ????????????????????????pcfg.setMaxStatements(maxStatements);
    ????????????????????????pcfg.setTestConnectionOnCheckout(validateConnection);
    ????????????????????????pcfg.setIdleConnectionTestPeriod(idleTestPeriod);
    ????????????????????????
    ?????????? //用第三方插件來初始化連接池,并向程序返回了JDBC的DataSource接口,使得任何第三方插件的上層程序都不必去引入相應(yīng)具體的第三方的類庫,實(shí)現(xiàn)的低耦合;
    ????????????????????????/*DataSource unpooled = DataSources.unpooledDataSource(
    ????????????????????????????????jdbcUrl, props.getProperty(Environment.USER), props.getProperty(Environment.PASS)
    ????????????????????????);*/
    ????????????????????????DataSource unpooled = DataSources.unpooledDataSource(jdbcUrl, connectionProps);
    ????????????????????????ds = DataSources.pooledDataSource(unpooled, pcfg);
    ????????????????????????
    ????????????????}
    ????????????????catch (Exception e) {
    ????????????????????????log.fatal("could not instantiate C3P0 connection pool", e);
    ????????????????????????throw new HibernateException("Could not instantiate C3P0 connection pool", e);
    ????????????????}
    ????????????????
    ????????????????String i = props.getProperty(Environment.ISOLATION);
    ????????????????if (i==null) {
    ????????????????????????isolation=null;
    ????????????????}
    ????????????????else {
    ????????????????????????isolation = new Integer(i);
    ????????????????????????log.info( "JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) );
    ????????????????}
    ????????????????
    ????????}

    public Connection getConnection() throws SQLException {
    ????????????????final Connection c = ds.getConnection();
    ????????//設(shè)置事務(wù)的隔離級(jí)別;
    ????????????????if (isolation!=null) c.setTransactionIsolation( isolation.intValue() );
    ????????????????if ( c.getAutoCommit() ) c.setAutoCommit(false);
    ????????????????return c;
    ????????}
    public void closeConnection(Connection conn) throws SQLException {
    ????????????????conn.close();
    ????????}
    public void close() {
    ????????????????try {
    ????????????????????????DataSources.destroy(ds);
    ????????????????}
    ????????????????catch (SQLException sqle) {
    ????????????????????????log.warn("could not destroy C3P0 connection pool", sqle);
    ????????????????}
    ????????}
    1.2????????class DriverManagerConnectionProvider implements ConnectionProvider


    public void configure(Properties props) throws HibernateException {
    ????????????????
    ????????????????String driverClass = props.getProperty(Environment.DRIVER);
    ????????????????
    ????????????????poolSize = PropertiesHelper.getInt(Environment.POOL_SIZE, props, 20); //default pool size 20
    ????????????????log.info("Using Hibernate built-in connection pool (not for production use!)");
    ????????????????log.info("Hibernate connection pool size: " + poolSize);
    ????????????????
    ????????????????isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props);
    ????????????????if (isolation!=null)
    ????????????????log.info( "JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) );
    ????????????????
    ????????????????if (driverClass==null) {
    ????????????????????????log.warn("no JDBC Driver class was specified by property " + Environment.DRIVER);
    ????????????????}
    ????????????????else {
    ????????????????????????try {
    ????????????????????????????????// trying via forName() first to be as close to DriverManager's semantics
    ????????????????????????????????Class.forName(driverClass);????????????????????????????????????????????????????????????????
    ????????????????????????}
    ????????????????????????catch (ClassNotFoundException cnfe) {
    ????????????????????????????????try {
    ????????????????????????????????????????ReflectHelper.classForName(driverClass);
    ????????????????????????????????} catch (ClassNotFoundException e) {
    ????????????????????????????????String msg = "JDBC Driver class not found: " + driverClass;
    ????????????????????????????????log.fatal(msg);
    ????????????????????????????????throw new HibernateException(msg);
    ????????????????????????????????}
    ????????????????????????}
    ????????????????}
    ????????????????
    ????????????????url = props.getProperty(Environment.URL);
    ????????????????if (url==null) {
    ????????????????????????String msg = "JDBC URL was not specified by property " + Environment.URL;
    ????????????????????????log.fatal(msg);
    ????????????????????????throw new HibernateException(msg);
    ????????????????}
    ????????????????
    ????????????????connectionProps = ConnectionProviderFactory.getConnectionProperties(props);
    ????????????????
    ????????????????log.info( "using driver: " + driverClass + " at URL: " + url );
    ????????????????log.info("connection properties: " + connectionProps);
    ????????????????
    ????????}
    ??
    ????????public Connection getConnection() throws SQLException {
    ????????????????
    ????????????????if ( log.isTraceEnabled() ) log.trace( "total checked-out connections: " + checkedOut );
    ????????????????//鎖死連接池,只需一個(gè)線程訪問,池中有連接就提供,沒有在新建一個(gè);
    ????????????????synchronized (pool) {
    ????????????????????????if ( !pool.isEmpty() ) {
    ????????????????????????????????int last = pool.size() - 1;
    ????????????????????????????????if ( log.isTraceEnabled() ) {
    ????????????????????????????????????????log.trace("using pooled JDBC connection, pool size: " + last);
    ????????????????????????????????????????checkedOut++;
    ????????????????????????????????}
    ????????????????????????????????Connection pooled = (Connection) pool.remove(last);
    ????????????????????????????????if (isolation!=null) pooled.setTransactionIsolation( isolation.intValue() );
    ????????????????????????????????if ( pooled.getAutoCommit() ) pooled.setAutoCommit(false);
    ????????????????????????????????return pooled;
    ????????????????????????}
    ????????????????}
    ????????????????
    ????????????????log.debug("opening new JDBC connection");
    ????????????????Connection conn = DriverManager.getConnection(url, connectionProps);
    ????????????????if (isolation!=null) conn.setTransactionIsolation( isolation.intValue() );
    ????????????????if ( conn.getAutoCommit() ) conn.setAutoCommit(false);

    ????????????????if ( log.isDebugEnabled() ) {
    ????????????????????????log.debug( "created connection to: " + url + ", Isolation Level: " + conn.getTransactionIsolation() );
    ????????????????}
    ????????????????if ( log.isTraceEnabled() ) checkedOut++;
    ????????????????
    ????????????????return conn;
    ????????}

    public void closeConnection(Connection conn) throws SQLException {
    ????????????????
    ????????????????if ( log.isDebugEnabled() ) checkedOut--;
    ????????????????//如果池中連接已滿就釋放連接,否則就加到連接池中;
    ????????????????synchronized (pool) {
    ????????????????????????int currentSize = pool.size();
    ????????????????????????if ( currentSize < poolSize ) {
    ????????????????????????????????if ( log.isTraceEnabled() ) log.trace("returning connection to pool, pool size: " + (currentSize + 1) );
    ????????????????????????????????pool.add(conn);
    ????????????????????????????????return;
    ????????????????????????}
    ????????????????}
    ????????????????
    ????????????????log.debug("closing JDBC connection");
    ????????????????
    ????????????????try {
    ????????????????????????conn.close();
    ????????????????}
    ????????????????catch (SQLException sqle) {
    ????????????????????????JDBCExceptionReporter.logExceptions(sqle);
    ????????????????????????throw sqle;
    ????????????????}
    ????????}

    public void close() {
    ????????????????//清空連接池;
    ????????????????log.info("cleaning up connection pool: " + url);
    ????????????????
    ????????????????Iterator iter = pool.iterator();
    ????????????????while ( iter.hasNext() ) {
    ????????????????????????try {
    ????????????????????????????????( (Connection) iter.next() ).close();
    ????????????????????????}
    ????????????????????????catch (SQLException sqle) {
    ????????????????????????????????log.warn("problem closing pooled connection", sqle);
    ????????????????????????}
    ????????????????}
    ????????????????pool.clear();
    ????????????????
    ????????}

    posted on 2006-04-23 13:02 chenhui 閱讀(749) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    介紹 IOC

    友情鏈接

    最新隨筆

    搜索

    積分與排名

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲国产午夜电影在线入口| 亚洲AV人人澡人人爽人人夜夜| 亚洲欧洲久久精品| 免费A级毛片无码专区| 亚洲国产精品SSS在线观看AV| 一区二区在线视频免费观看| 尤物永久免费AV无码网站| 亚洲AV无码AV男人的天堂不卡| 国产精品无码一区二区三区免费 | 亚洲三级高清免费| 99精品国产免费久久久久久下载| 亚洲欧洲自拍拍偷午夜色| 国产日本一线在线观看免费 | 亚洲国产成人无码AV在线影院| 女人被免费视频网站| 亚洲高清一区二区三区电影| 国产男女性潮高清免费网站| 男女作爱免费网站| 亚洲国产精品一区二区久久hs| 99久久国产免费-99久久国产免费| 久久亚洲美女精品国产精品 | 免费看又黄又爽又猛的视频软件| 久久亚洲2019中文字幕| 日本高清高色视频免费| 亚洲一区二区三区在线| 四虎影视永久免费视频观看| 伊人免费在线观看| 亚洲成人网在线观看| 在线观看亚洲免费| 国产成人免费ā片在线观看老同学| 亚洲高清在线视频| 毛片免费视频播放| 成全视成人免费观看在线看| 18gay台湾男同亚洲男同| 永久黄网站色视频免费直播| 成人免费无码H在线观看不卡| 亚洲午夜久久久久久尤物| 免费人成网站在线高清| 久久久精品2019免费观看| AV激情亚洲男人的天堂国语| 亚洲av永久无码精品表情包|