Wiki source code of Multi-DataSource pooling with JDBC-Provider
Version 3.1 by christoph_lechleitner@iteg_at on 2013-07-09 03.09:12
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 | |||
| |
2.1 | 13 | This package uses the configuration PID org.clazzes.jdbc.provider and may be configured with keys using the following patterns: |
| |
1.1 | 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 | |((( | ||
| |
2.1 | 34 | {{code language="none"}} |
| |
3.1 | 35 | datasource.<datasourcename>.username |
| |
2.1 | 36 | {{/code}} |
| |
1.1 | 37 | )))|((( |
| 38 | JDBC User | ||
| 39 | ))) | ||
| 40 | |((( | ||
| |
2.1 | 41 | {{code language="none"}} |
| |
1.1 | 42 | datasource.<datasourcename>.password |
| |
2.1 | 43 | {{/code}} |
| |
1.1 | 44 | )))|((( |
| 45 | JDBC Password | ||
| 46 | ))) | ||
| 47 | |((( | ||
| |
2.1 | 48 | {{code language="none"}} |
| |
1.1 | 49 | datasource.<datasourcename>.validationQuery |
| |
2.1 | 50 | {{/code}} |
| |
1.1 | 51 | )))|((( |
| 52 | Validation query, executed to ensure the application receives a valid connection | ||
| 53 | ))) | ||
| 54 | |||
| 55 | 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. | ||
| 56 | |||
| |
2.1 | 57 | Typical JDBC URLs and validation queries can be found in our [[doc:KH.Database Tips.JDBC Snippets.WebHome]]. |
| 58 | |||
| |
1.1 | 59 | == {{id name="Multi-DataSourcepoolingwithJDBC-Provider-Configurationexample"/}}Configuration example == |
| 60 | |||
| 61 | ((( | ||
| 62 | Sample configuration file {{code language="none"}}/etc/apache-karaf/org.clazzes.jdbc.provider.cfg{{/code}}: | ||
| 63 | ))) | ||
| 64 | |||
| 65 | {{code language="none"}} | ||
| 66 | datasource.JPTEST.url = jdbc:mysql://localhost/JPTEST | ||
| 67 | datasource.JPTEST.username = jptest | ||
| 68 | datasource.JPTEST.password = jptest321 | ||
| 69 | datasource.JPTEST.validationQuery = SELECT 1 | ||
| 70 | datasource.SPECTRUM.url = jdbc:oracle:thin:@10.1.2.3:1521:XE | ||
| 71 | datasource.SPECTRUM.maxActive = 8 | ||
| 72 | datasource.SPECTRUM.username = FOO | ||
| 73 | datasource.SPECTRUM.password = bar | ||
| 74 | datasource.SPECTRUM.validationQuery = select SYSDATE from DUAL | ||
| 75 | {{/code}} | ||
| 76 | |||
| |
2.1 | 77 | (% style="color: rgb(0,0,0);font-size: 24.0px;line-height: 1.25;" %)Developer Snippets |
| |
1.1 | 78 | |
| 79 | == {{id name="Multi-DataSourcepoolingwithJDBC-Provider-pom.xmlSnippets"/}}pom.xml Snippets == | ||
| 80 | |||
| 81 | It's important for the maven-bundle-plugin to import the Package {{code language="none"}}javax.sql{{/code}}: | ||
| 82 | |||
| 83 | {{code language="none"}} | ||
| 84 | ... | ||
| 85 | <build> | ||
| 86 | <plugins> | ||
| 87 | <plugin> | ||
| 88 | <groupId>org.apache.felix</groupId> | ||
| 89 | <artifactId>maven-bundle-plugin</artifactId> | ||
| 90 | <configuration> | ||
| 91 | <instructions> | ||
| 92 | ... | ||
| 93 | <Import-Package>*,javax.sql</Import-Package> | ||
| 94 | </instructions> | ||
| 95 | </configuration> | ||
| 96 | </plugin> | ||
| 97 | ... | ||
| 98 | </plugin> | ||
| 99 | ... | ||
| 100 | </build> | ||
| 101 | {{/code}} | ||
| 102 | |||
| 103 | But that's all you need! | ||
| 104 | |||
| 105 | == {{id name="Multi-DataSourcepoolingwithJDBC-Provider-BlueprintSnippets"/}}Blueprint Snippets == | ||
| 106 | |||
| 107 | {{code language="none"}} | ||
| 108 | <bp:bean id="configProps" class="org.clazzes.util.osgi.ConfigPropertyAccessor" init-method="createDefaultConfig"> | ||
| 109 | ... | ||
| 110 | <bp:property name="defaultValues"> | ||
| 111 | <bp:map> | ||
| 112 | <bp:entry key="datasourceName" value="MYDATASOURCE"/> | ||
| 113 | ... | ||
| 114 | </bp:map> | ||
| 115 | </bp:property> | ||
| 116 | </bp:bean> | ||
| 117 | ... | ||
| 118 | <bp:bean id="datasourceName" factory-ref="configProps" factory-method="getProperty"> | ||
| 119 | <bp:argument value="datasourceName" /> | ||
| 120 | </bp:bean> | ||
| 121 | ... | ||
| 122 | <bp:bean id="datasourceMap" class="org.clazzes.util.osgi.ServiceMap" init-method="initialize"> | ||
| 123 | <bp:property name="bundle" ref="blueprintBundle"/> | ||
| 124 | <bp:property name="keyProperty" value="datasource.name"/> | ||
| 125 | <bp:property name="serviceInterface" value="javax.sql.DataSource"/> | ||
| 126 | </bp:bean> | ||
| 127 | <bp:bean id="dataSource" factory-ref="datasourceMap" factory-method="getServiceProxy"> | ||
| 128 | <bp:argument ref="datasourceName"/> | ||
| 129 | </bp:bean> | ||
| 130 | ... | ||
| 131 | <bp:bean id="myDao" ...> | ||
| 132 | <bp:property name="dataSource" ref="dataSource" /> | ||
| 133 | </bp:bean> | ||
| 134 | {{/code}} | ||
| 135 | |||
| 136 | The blueprint xml can and should be kept free from any other JDBC stuff like URLs, driver magic and the like. | ||
| 137 | |||
| 138 | (% class="syntaxhighlighter nogutter java" %) | ||
| 139 | ((( | ||
| 140 | |||
| 141 | ))) |