对hibernate3.3rc扩展

编程

最近维护老项目,需要用到druid监控,但是druid最低兼容hibernate 4,没办法,自己动手丰衣足食。

基于com.alibaba.druid.support.hibernate.DruidConnectionProvider改的,实现ConnectionProvider接口

因为要使用druid数据源,改动如下

/*

* Copyright 1999-2018 Alibaba Group Holding Ltd.

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.alibaba.druid.support.hibernate;

import com.alibaba.druid.pool.DruidDataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import org.hibernate.HibernateException;

import org.hibernate.connection.ConnectionProvider;

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.Properties;

public class DruidConnectionProvider1 implements ConnectionProvider {

private DruidDataSource dataSource;

@Override

public void configure(Properties properties) throws HibernateException {

String druid = properties.getProperty("druid");//得到druid的属性文件,配置到hibernate xml里

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(druid);

Properties druidProperties = new Properties();

try {

druidProperties.load(inputStream);

// properties.putAll(druidProperties);

/*dataSource=new DruidDataSource();

dataSource.configFromPropety(druidProperties);*/

dataSource = (DruidDataSource)DruidDataSourceFactory.createDataSource(druidProperties);

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

@Override

public Connection getConnection() throws SQLException {

return dataSource.getConnection();

}

@Override

public void closeConnection(Connection connection) throws SQLException {

connection.close();

}

@Override

public void close() throws HibernateException {

((DruidDataSource)dataSource).close();;

}

@Override

public boolean supportsAggressiveRelease() {

return false;

}

}

后来想着项目里有一个老的数据源,能把两个合并到一起就更好了。

/*

* Copyright 1999-2018 Alibaba Group Holding Ltd.

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package com.alibaba.druid.support.hibernate;

import com.alibaba.druid.pool.DruidDataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import com.topsoft.domain.common.DaoHelp;

import org.hibernate.HibernateException;

import org.hibernate.connection.ConnectionProvider;

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.Properties;

public class DruidConnectionProvider implements ConnectionProvider {

private DruidDataSource dataSource;

@Override

public void configure(Properties properties) throws HibernateException {

}

@Override

public Connection getConnection() throws SQLException {

return DaoHelp.getInstance().getSession().connection();//项目封装的一个工具类,可以自己封装。

}

@Override

public void closeConnection(Connection connection) throws SQLException {

connection.close();

}

@Override

public void close() throws HibernateException {

}

@Override

public boolean supportsAggressiveRelease() {

return false;

}

}

最终问题解决。

最后在给一个小提示,如果配置完druid连接池,发现没有监控sql  ,添加属性filters:stat

以上是 对hibernate3.3rc扩展 的全部内容, 来源链接: utcz.com/z/511168.html

回到顶部