Skip to content

Bridje JDBC

gilbertovento edited this page Mar 28, 2016 · 28 revisions

Introduction

Bridje JDBC can be used with maven from central repository.

    <dependencies>
        ....
        <dependency>
            <groupId>org.bridje</groupId>
            <artifactId>bridje-jdbc</artifactId>
            <version>0.1.5-1</version>
        </dependency>
        ....
    </dependencies>

Introduction

Bridje JDBC is a pooled jdbc connections data source implementation, with offers a way to configure and discover this data sources with convinient methods an filosofy. This API use Bridje Cfg and Bridje Ioc to configure and discover the data sources. The main services this API provides is the JdbcService wich can create and retreave pooled data sources from the 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 whant.

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

Creating DataSources programatically

One way to get a datasourct is by hardcoding 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 conection.
    config.setDriver("org.h2.Driver");
    //Set the JDBC url connection string.
    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("");
    //Create the DataSource
    DataSource ds = jdbc.createDataSource(config);
    ....
    //Use the DataSrouce
    ....

Configuring and retriving DataSrouces

Another way to get a DataSource is to delegate the configuration to the user and write the code to retrive the instance at runtime. The JdbcServic use the Bridje Cfg API to discover the configuration for the given DataSource. The configuration looks like this.

<jdbc:jdbc
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:jdbc='http://www.bridje.org/schemas/jdbc'>
    <jdbc:datasources>
        <jdbc:name>H2TestDataSource</jdbc:name>
        <jdbc:driver>org.h2.Driver</jdbc:driver>
        <jdbc:url>jdbc:h2:./target/dbtest</jdbc:url>
        <jdbc:username>sa</jdbc:username>
        <jdbc:password></jdbc:password>
    </jdbc:datasources>
</jdbc:jdbc>

This configuration must be put into a file named jdbc.xml into a folder that the ConfigService has access to through a ConfigRepository. Check the Bridje Cfg API to know how to do this.

At runtime you can retrive 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.
    DataSource ds = jdbc.getDataSource("H2TestDataSource");
    ....
    //Use the DataSrouce
    ....

Closing DataSrouces

Normally you won have to relese the DataSources, seens provably you´ll whant to keep they 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 JdbcInterface.

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

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

The closeAllDataSource is mean to be use as a final release of all the DataSources that the JdbcService is managing internally. Use this method only if you have no plan of using the DataSources no fuether.

    JdbcService jdbc = Ioc.context().find(JdbcService.class);
    DataSource ds1 = jdbc.getDataSource("DS1");
    DataSource ds1 = jdbc.getDataSource("DS2");
    ....
    jdbc.closeAllDataSource(ds);
    ....
Clone this wiki locally