数据库
首页 > 数据库> > 使用HSQLDB和Spring嵌入式数据库启用连接池

使用HSQLDB和Spring嵌入式数据库启用连接池

作者:互联网

最近,我一直在尝试为我们在生产中使用Oracle DB的应用程序之一实现基于HSQLDB的内存数据库.该应用程序使用spring框架.但是,我必须以编程方式实现数据源bean,因为我们使用现有的SQL DDL语句(Oracle查询),因此必须以编程方式删除命名空间之类的构造才能在HSQLDB上运行.

我使用EmbeddedDatabaseBuilder(ResourceLoader)初始化数据库.

现在我的问题是我现在想要使用say c3p0添加连接池.
通常我会用

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="HSQLDB driver path" /> (this is just for representation)
    <property name="jdbcUrl" value="${xxx.jdbcUrl}" />
    <property name="user" value="${xxx.username}" />
    <property name="password" value="${xxx.password}" />
    <property name="minPoolSize" value="1" />
    <property name="maxPoolSize" value="3" />
    <property name="maxIdleTime" value="20" />
</bean>

但是,我很困惑如何在使用Spring嵌入式数据库时定义它.

免责声明:我真的很喜欢春天.

解决方法:

在此link之后:

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {
    public static ComboPooledDataSource newDefaultDS() {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setMinPoolSize(1);
        dataSource.setMaxPoolSize(1);
        dataSource.setMaxIdleTime(20);

        return dataSource;
    }
}

import java.beans.PropertyVetoException;
import java.sql.Driver;

import org.springframework.jdbc.datasource.embedded.ConnectionProperties;
import org.springframework.jdbc.datasource.embedded.DataSourceFactory;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class ComboPoolDataSourceFactory implements DataSourceFactory,
        ConnectionProperties {
    private final ComboPooledDataSource dataSource;

    public ComboPoolDataSourceFactory() {
        this(C3P0Utils.newDefaultDS());
    }

    public ComboPoolDataSourceFactory(ComboPooledDataSource dataSource) {
        assert dataSource != null;
        this.dataSource = dataSource;
    }

    public ConnectionProperties getConnectionProperties() {
        return this;
    }

    public ComboPooledDataSource getDataSource() {
        return dataSource;
    }

    public void setUsername(String username) {
        dataSource.setUser(username);
    }

    public void setPassword(String password) {
        dataSource.setPassword(password);
    }

    public void setUrl(String url) {
        dataSource.setJdbcUrl(url);
    }

    public void setDriverClass(Class<? extends Driver> driverClass) {
        try {
            dataSource.setDriverClass(driverClass.getName());
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
}

import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;

public class EmbeddedC3P0DatabaseBuilder extends EmbeddedDatabaseBuilder {
    public EmbeddedC3P0DatabaseBuilder() {
        setDataSourceFactory(new ComboPoolDataSourceFactory());
    }
}

一个简短的用法示例:

EmbeddedC3P0DatabaseBuilder builder = new EmbeddedC3P0DatabaseBuilder();
        EmbeddedDatabase db = builder
                .setType(EmbeddedDatabaseType.H2)
                .addScript("setup-tables.sql")
                .build();

        JdbcTemplate template = new JdbcTemplate(db);
                ....
        db.shutdown();

标签:c3p0,sql,spring,connection-pooling,hsqldb
来源: https://codeday.me/bug/20190830/1770449.html