-
Notifications
You must be signed in to change notification settings - Fork 6
Bridje JDBC
Bridje JDBC can be used with maven from central repository.
<dependencies>
....
<dependency>
<groupId>org.bridje</groupId>
<artifactId>bridje-jdbc</artifactId>
<version>0.2.2</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 Cfg 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.
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;
.....
}
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.
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 DataSource
....
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 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'
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>
<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:datasource>
</jdbc:datasources>
</jdbc:jdbc>
This configuration must be put into a file named jdbc.xml into a folder the ConfigService can access through a ConfigRepository. Check the Bridje Cfg API for further insight on how to do this.
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.
DataSource ds = jdbc.getDataSource("H2TestDataSource");
....
//Use the DataSrouce
....
Normally you won't have to release the DataSources, since provably 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 JdbcInterface.
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 interact with the DataSources.
JdbcService jdbc = Ioc.context().find(JdbcService.class);
DataSource ds1 = jdbc.getDataSource("DS1");
DataSource ds1 = jdbc.getDataSource("DS2");
....
jdbc.closeAllDataSource(ds);
....