My CQ5 application needs to connect to an external RDBMS. How do I configure and connect to a JDBC DataSource?
As an example we will create a JDBC DataSource to connect to H2 database engine
Note that the blank line at the end of a jar manifest file is required.
jar -cfm org.h2.jdbc.driver.extension-1.1.119.jar h2ext.mf
Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.h2.jdbc.driver.extension Bundle-Version: 1.1.119 Fragment-Host: system.bundle; extension:=framework Bundle-Name: H2 JDBC Driver Extension Bundle-Description: Bundle exporting H2 JDBC Driver Export-Package: org.h2
Naviagte to the Cofiguration Manager in the Felix console (http://localhost:4502/system/console/configMgr). Use JDBC Connection Pool configuration factory to create a new connection pool. For Example it might have the following settings.
JDBC driver class: org.h2.Driver JDBC connection URI: jdbc:h2:~/test Usename: sa Password: Validation query: Pool size: 10 Pool wait (msec): 100 Datasource name: h2DataSource
Get an instance of the DataSource.
Once configured the DataSource instance is acquired by calling getDataSource(String datasourceName) method of DataSourcePool service (com.day.commons.datasource.poolservice.DataSourcePool) and passing it the datasource name.
In this example we are creating a simple OSGi service which can be used to get an instance of a DataSource. This example is not realistic, since it basically reinvents the same API as com.day.commons.datasource.poolservice.DataSourcePool itself without adding any functionality. But it shows how DataSourcePool can be used in your own code.
Lets first create an interface for our service.
package com.day.test.datasource; import javax.sql.DataSource; public interface DatasourceUtil { public DataSource getDataSource(String dataSourceName); }
Now the implementation. As you can see besides hiding the DataSourceNotFoundException it does not add any value :).
package com.day.test.datasource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.sql.DataSource; import com.day.commons.datasource.poolservice.DataSourceNotFoundException; import com.day.commons.datasource.poolservice.DataSourcePool; /** * * This class is an example if how DataSourcePool can be used to obtain the DataSource. * * @scr.component immediate="true" metatype="no" * @scr.service interface="DatasourceUtil" * @scr.property name="service.description" value="Data Source lookup example" * @scr.property name="service.vendor" value="Day Software" */ public class DatasourceUtilImpl implements DatasourceUtil { private static final Logger log = LoggerFactory.getLogger( DatasourceUtilImpl.class); /** @scr.reference policy="static" */ private DataSourcePool dataSourceService; public DataSource getDataSource(String dataSourceName) { log.info("Using DataSourcePool service lookup " + "to get connection pool " + dataSourceName); DataSource dataSource = null; try { dataSource = (DataSource) dataSourceService.getDataSource( dataSourceName); } catch (DataSourceNotFoundException e) { log.error("Unable to find datasource {}.", dataSourceName, e); } return dataSource; } }
This class is using OSGi Declarative Services specification to get an instance of DataSourcePool service and then calls its getDataSource(dataSourceName) to get the DataSource. Maven SCR Plugin is using the the scr java annotations to generate the XML descriptors for OSGi Declarative Services. The attached zip file contain the example POM.
You can download a complete example here: datasourceutil.zip. It includes a maven POM to build it. In order to build this project using POM provided please refer to How to use CQ5 as maven repository.
This is a CRX package that contains a JSP example - datasource-example-jsp.zip. It"s a CQ component that can be added to any page with a paragraph system. It can be used to test your DataSource connection.
Added is a sample bundle to build with CRXDE / CRXDE Lite that contains the oracle driverinstall the oracle_example_driver.zip, have a look at the /apps/oracle.In the Activator.java it explicitly load the driver class (it may not be required in future releases).Make sure you have the latest day.commons.datasource.jdbcpool (1.0.8) installed before using it.
CQ5.2.1, CQ5.3
Lejupielādēt
Lejupielādēt
Lejupielādēt
Lejupielādēt
Lejupielādēt
Lejupielādēt
Lejupielādēt
Lejupielādēt
Pierakstieties savā kontā