Wiki source code of Multi-DataSource pooling with JDBC-Provider
Version 1.1 by christoph_lechleitner@iteg_at on 2013-07-09 02.32:04
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
1.1 | 1 | = {{id name="Multi-DataSourcepoolingwithJDBC-Provider-Introduction"/}}Introduction = |
| 2 | |||
| 3 | Many OSGi bundles require a pool of database connections, and usually the implementing it themselfes. | ||
| 4 | |||
| 5 | In larger multi-package projects resp. OSGi container installations, a whole list of bundles require access to one and the same data source. | ||
| 6 | |||
| 7 | This not only is a configuration nightmare, it also produces an unnecessary large number of unnecessary large pools. | ||
| 8 | |||
| 9 | The new OSGi bundle {{code language="none"}}org.clazzes.util.jdbc-provider{{/code}} allows to configure multiple datasources, which are exported as OSGi services implementing the interface {{code language="none"}}javax.sql.DataSource{{/code}}. | ||
| 10 | |||
| 11 | = {{id name="Multi-DataSourcepoolingwithJDBC-Provider-ConfiguringJDBC-ProviderDataSources"/}}Configuring JDBC-Provider DataSources = | ||
| 12 | |||
| 13 | This package uses the configuration PID org.clazzes.jdbc.provider and may be configured with keys using the following pattern: | ||
| 14 | |||
| 15 | |=((( | ||
| 16 | (% class="tablesorter-header-inner" %) | ||
| 17 | ((( | ||
| 18 | Key | ||
| 19 | ))) | ||
| 20 | )))|=((( | ||
| 21 | (% class="tablesorter-header-inner" %) | ||
| 22 | ((( | ||
| 23 | Description | ||
| 24 | ))) | ||
| 25 | ))) | ||
| 26 | |((( | ||
| 27 | {{code language="none"}} | ||
| 28 | datasource.<datasourcename>.url | ||
| 29 | {{/code}} | ||
| 30 | )))|((( | ||
| 31 | JDBC URL | ||
| 32 | ))) | ||
| 33 | |((( | ||
| 34 | datasource.<datasourcename>.user | ||
| 35 | )))|((( | ||
| 36 | JDBC User | ||
| 37 | ))) | ||
| 38 | |((( | ||
| 39 | datasource.<datasourcename>.password | ||
| 40 | )))|((( | ||
| 41 | JDBC Password | ||
| 42 | ))) | ||
| 43 | |((( | ||
| 44 | datasource.<datasourcename>.validationQuery | ||
| 45 | )))|((( | ||
| 46 | Validation query, executed to ensure the application receives a valid connection | ||
| 47 | ))) | ||
| 48 | |((( | ||
| 49 | datasource.<datasourcename>.maxActive | ||
| 50 | )))|((( | ||
| 51 | Optional | ||
| 52 | ))) | ||
| 53 | |||
| 54 | For more supported key patterns take a look at the JavaDoc of the [[org.clazzes.util.jdbc.provider.JdbcProvider>>url:http://svn.clazzes.org/svn/util/trunk/jdbc-provider/src/main/java/org/clazzes/util/jdbc/provider/JdbcProvider.java||shape="rect"]] class. | ||
| 55 | |||
| 56 | == {{id name="Multi-DataSourcepoolingwithJDBC-Provider-Configurationexample"/}}Configuration example == | ||
| 57 | |||
| 58 | ((( | ||
| 59 | Sample configuration file {{code language="none"}}/etc/apache-karaf/org.clazzes.jdbc.provider.cfg{{/code}}: | ||
| 60 | ))) | ||
| 61 | |||
| 62 | |||
| 63 | |||
| 64 | {{code language="none"}} | ||
| 65 | datasource.JPTEST.url = jdbc:mysql://localhost/JPTEST | ||
| 66 | datasource.JPTEST.username = jptest | ||
| 67 | datasource.JPTEST.password = jptest321 | ||
| 68 | datasource.JPTEST.validationQuery = SELECT 1 | ||
| 69 | datasource.SPECTRUM.url = jdbc:oracle:thin:@10.1.2.3:1521:XE | ||
| 70 | datasource.SPECTRUM.maxActive = 8 | ||
| 71 | datasource.SPECTRUM.username = FOO | ||
| 72 | datasource.SPECTRUM.password = bar | ||
| 73 | datasource.SPECTRUM.validationQuery = select SYSDATE from DUAL | ||
| 74 | {{/code}} | ||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | = {{id name="Multi-DataSourcepoolingwithJDBC-Provider-DeveloperSnippets"/}}Developer Snippets = | ||
| 79 | |||
| 80 | == {{id name="Multi-DataSourcepoolingwithJDBC-Provider-pom.xmlSnippets"/}}pom.xml Snippets == | ||
| 81 | |||
| 82 | It's important for the maven-bundle-plugin to import the Package {{code language="none"}}javax.sql{{/code}}: | ||
| 83 | |||
| 84 | {{code language="none"}} | ||
| 85 | ... | ||
| 86 | <build> | ||
| 87 | <plugins> | ||
| 88 | <plugin> | ||
| 89 | <groupId>org.apache.felix</groupId> | ||
| 90 | <artifactId>maven-bundle-plugin</artifactId> | ||
| 91 | <configuration> | ||
| 92 | <instructions> | ||
| 93 | ... | ||
| 94 | <Import-Package>*,javax.sql</Import-Package> | ||
| 95 | </instructions> | ||
| 96 | </configuration> | ||
| 97 | </plugin> | ||
| 98 | ... | ||
| 99 | </plugin> | ||
| 100 | ... | ||
| 101 | </build> | ||
| 102 | {{/code}} | ||
| 103 | |||
| 104 | But that's all you need! | ||
| 105 | |||
| 106 | == {{id name="Multi-DataSourcepoolingwithJDBC-Provider-BlueprintSnippets"/}}Blueprint Snippets == | ||
| 107 | |||
| 108 | {{code language="none"}} | ||
| 109 | <bp:bean id="configProps" class="org.clazzes.util.osgi.ConfigPropertyAccessor" init-method="createDefaultConfig"> | ||
| 110 | ... | ||
| 111 | <bp:property name="defaultValues"> | ||
| 112 | <bp:map> | ||
| 113 | <bp:entry key="datasourceName" value="MYDATASOURCE"/> | ||
| 114 | ... | ||
| 115 | </bp:map> | ||
| 116 | </bp:property> | ||
| 117 | </bp:bean> | ||
| 118 | ... | ||
| 119 | <bp:bean id="datasourceName" factory-ref="configProps" factory-method="getProperty"> | ||
| 120 | <bp:argument value="datasourceName" /> | ||
| 121 | </bp:bean> | ||
| 122 | ... | ||
| 123 | <bp:bean id="datasourceMap" class="org.clazzes.util.osgi.ServiceMap" init-method="initialize"> | ||
| 124 | <bp:property name="bundle" ref="blueprintBundle"/> | ||
| 125 | <bp:property name="keyProperty" value="datasource.name"/> | ||
| 126 | <bp:property name="serviceInterface" value="javax.sql.DataSource"/> | ||
| 127 | </bp:bean> | ||
| 128 | <bp:bean id="dataSource" factory-ref="datasourceMap" factory-method="getServiceProxy"> | ||
| 129 | <bp:argument ref="datasourceName"/> | ||
| 130 | </bp:bean> | ||
| 131 | ... | ||
| 132 | <bp:bean id="myDao" ...> | ||
| 133 | <bp:property name="dataSource" ref="dataSource" /> | ||
| 134 | </bp:bean> | ||
| 135 | {{/code}} | ||
| 136 | |||
| 137 | The blueprint xml can and should be kept free from any other JDBC stuff like URLs, driver magic and the like. | ||
| 138 | |||
| 139 | (% class="syntaxhighlighter nogutter java" %) | ||
| 140 | ((( | ||
| 141 | |||
| 142 | ))) |