Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Bridje JDBC

Gilberto Vento edited this page Oct 12, 2019 · 1 revision

Introduction

Bridje JDBC can be used with maven from central repository.

    <dependencies>
        ....
        <dependency>
            <groupId>org.bridje</groupId>
            <artifactId>bridje-jdbc</artifactId>
            <version>${bridje.version}</version>
        </dependency>
        ....
    </dependencies>

Bridje JDBC is a pooled JDBC connections data source implementation, which offers a way to configure and discover such data sources with convenient methods and a fresh philosophy. This API uses Bridge VFS and Bridge IoC to configure and discover the data sources. The main service this API provides is the JdbcService which can create and retrieve pooled data sources from a given configuration.

JdbcService

To be able to use the JdbcService you must find it through the standard service discovery mechanism like this:

import org.bridje.jdbc.JdbcService;
.....
JdbcService jdbcServ = Ioc.context().find(JdbcService.class);
.....

or by injecting it into any component you want:

import org.bridje.jdbc.JdbcService;
.....
@Component
class MyCompo
{
    @Inject
    private JdbcService jdbcServ;
.....
}

Creating Data Sources programmatically

One way to get a data source is by hard-coding the JDBC connection configuration into the code and use the createDataSource method like this:

    //Get the JdbcService instance.       
    JdbcService jdbc = Ioc.context().find(JdbcService.class);
    //Create a new DataSource configuration object.
    DataSourceConfig config = new DataSourceConfig();
    //Set the JDBC driver for the connection (ex: com.mysql.jdbc.Driver for MySQL server).
    config.setDriver("org.h2.Driver");
    //Set the JDBC url connection string (ex: jdbc:mysql://localhost:3306/database).
    config.setUrl("jdbc:h2:./target/h2testdb");
    //Set a valid database user with the required privileges on the target database.
    config.setUser("sa");
    //Set the password.
    config.setPassword("");
    //Sets the maximun numer of configurations allowed in the datasource.
    config.setMaxConnections(8);
    //The minimun amount of seconds a connection can be unused before it is released.
    config.setIdleTime(600);
    //The number of connections to keep in the datasource even if they are iddle.
    config.setMinConnections(3);
    //Create the DataSource, and now we can use the DataSource.
    DataSource ds = jdbc.createDataSource(config);

Configuring Data Sources

Another way to get a DataSource is to delegate the configuration to the user and write the code to retrieve the instance at run-time. The JdbcService use the Bridge VFS API to discover the configuration for the given DataSource. The configuration looks as follows.

<jdbc:jdbc
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:jdbc='http://www.bridje.org/schemas/jdbc'
    xsi:schemaLocation='http://www.bridje.org/schemas/jdbc http://repo1.maven.org/maven2/org/bridje/bridje-jdbc/0.2.2/bridje-jdbc-0.2.2-config.xsd'>
    <jdbc:datasources>
        <jdbc:datasource>
            <jdbc:name>H2TestDataSource</jdbc:name>
            <!-- Set the JDBC driver for the connection (ex: com.mysql.jdbc.Driver for MySQL server). -->
            <jdbc:driver>org.h2.Driver</jdbc:driver>
            <!-- Set the JDBC url connection string (ex: jdbc:mysql://localhost:3306/database). -->
            <jdbc:url>jdbc:h2:./target/dbtest</jdbc:url>
            <!-- Set a valid database user with the required privileges on the target database. -->
            <jdbc:username>sa</jdbc:username>
            <!-- Set the password. -->
            <jdbc:password></jdbc:password>
            <!-- Sets the maximun numer of configurations allowed in the datasource. -->
            <jdbc:maxConnections>8</jdbc:maxConnections>
            <!-- The minimun amount of seconds a connection can be unused before it is released. -->
            <jdbc:idleTime>600</jdbc:idleTime>
            <!-- The number of connections to keep in the datasource even if they are iddle. -->
            <jdbc:minConnections>3</jdbc:minConnections>
        <jdbc:datasource>
    </jdbc:datasources>
</jdbc:jdbc>

This configuration must be put into a file named jdbc.xml into a the etc virtual folder, you can do this at runtime by mounting a valid source into the /etc virtual file with the Bridje-VFS API.

//Mounting the configuration folder to /etc
//We assume that "path/to/my/config/folder" will contain the jdbc.xml file.
new VFile("/etc").mount(new FileSource("path/to/my/config/folder"));

Notice that the JDBC API will load its configuration from the virtual /etc/jdbc.xml virtual file. You must ensure that the file exist or none DataSource will be available by default.

Retrieving configured DataSources.

At run-time you can retrieve the configured DataSource object by using the getDataSource method from JdbcService.

    //Get the JdbcService instance.
    JdbcService jdbc = Ioc.context().find(JdbcService.class);
    //Get the DataSource instance by the name, and now we can use the DataSource.
    DataSource ds = jdbc.getDataSource("H2TestDataSource");

Notice that the name of the DataSource must be provided, if the DataSource was configured with the jdbc.xml file, it will be returned by this method, if the DataSource was not configured or the API cannot load the jdbc.xml file this method will return null.

Closing DataSources

Normally you won't have to release the DataSource, since probably you'll want to keep them alive as long as your application is running, but in case you need to do it manually, you can use the closeDataSource and closeAllDataSource methods from the JdbcService.

The closeDataSource Method will close the given DataSource, it must be a DataSource created with the createDataSource method or otherwise it will fail, throwing an invalid argument exception. The DataSource must not be already closed.

    JdbcService jdbc = Ioc.context().find(JdbcService.class);
    DataSource ds = jdbc.createDataSource(config);             
    //Close the DataSource.
    jdbc.closeDataSource(ds);

The closeAllDataSource is meant to be used as a final release procedure for all the DataSources that the JdbcService is managing internally. Use this method only if you have no further intentions of interacting with the DataSources.

    JdbcService jdbc = Ioc.context().find(JdbcService.class);
    DataSource ds1 = jdbc.getDataSource("DS1");
    DataSource ds1 = jdbc.getDataSource("DS2");
    ....
    //Close all DataSource.
    jdbc.closeAllDataSource();
    ....

After the use of closeDataSource or closeAllDataSource the closed DataSource will no longer work, any attempt to interact with this datasources or its connections will result in a SQLException.