Extending hibernate 3.3 rc

Keywords: Programming Druid Java Hibernate SQL

Recently, druid monitoring is needed for maintenance of old projects, but druid is at least compatible with hibernate 4. There is no way to do this. You can do it by yourself.

Based on com.alibaba.druid.support.hibernate.DruidConnectionProvider, the ConnectionProvider interface is implemented

To use the druid data source, make the following changes

/*
 * 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");//Get the property file of druid and configure it in 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;
    }
}

Later, I thought that there was an old data source in the project, so it would be better to merge the two together.

/*
 * 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();//A tool class encapsulated by a project, which can be encapsulated by itself.
    }

    @Override
    public void closeConnection(Connection connection) throws SQLException {
        connection.close();
    }

    @Override
    public void close() throws HibernateException {

    }

    @Override
    public boolean supportsAggressiveRelease() {
        return false;
    }
}

Final problem solving.

Finally, a small tip is given. If the druid connection pool is configured, and sql is not monitored, add the attribute filters:stat

Posted by tcollie on Tue, 26 Nov 2019 11:31:39 -0800