-
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.3</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.
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:
JdbcService jdbc = Ioc.context().find(JdbcService.class); //Get the JdbcService instance.
DataSourceConfig config = new DataSourceConfig(); //Create a new DataSource configuration object.
config.setDriver("org.h2.Driver"); //Set the JDBC driver for the connection (ex: com.mysql.jdbc.Driver for MySQL server).
config.setUrl("jdbc:h2:./target/h2testdb"); //Set the JDBC url connection string (ex: jdbc:mysql://localhost:3306/database).
config.setUser("sa"); //Set a valid database user with the required privileges on the target database.
config.setPassword(""); //Set the password.
DataSource ds = jdbc.createDataSource(config); //Create the DataSource, and now we can 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 VFS 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 the etc virtual folder, you can do this at runtime like this:
Ioc.context().find(VfsService.class).mountFile("/etc", new File("path/to/my/config/folder"));
At run-time you can retrieve the configured DataSource object by using the getDataSource method from JdbcService:
JdbcService jdbc = Ioc.context().find(JdbcService.class); //Get the JdbcService instance.
DataSource ds = jdbc.getDataSource("H2TestDataSource"); //Get the DataSource instance by the name, and now we can use the DataSource.
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);
jdbc.closeDataSource(ds); //Close the DataSource.
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(); //Close all DataSource.
....