Sometimes you write an application relying on multiple datasources
While they use different drivers (I mean different database driver) or the same version it is fine but when it is not the case what to do? (let take the exemple of two not compatible Oracle drivers).
Before digging the solution let’s remind you how TomEE allows you to define resources. The idea is to get a clear separation of concern between runtime environment and development one so you can define resources (DataSource here) outside the application in tomee.xml (or system.properties). If needed you can of course define it in the application but for prod environment this doesn’t always make sense.
Here a sample:
<Resource id="ds" type="DataSource"> JdbcDriver = oracle.jdbc.OracleDriver JdbcUrl = jdbc:oracle:thin:@localhost:1521:xe UserName = sys Password = pwd </Resource>
Of course password can be encrypted with a custom or not algorithm and you can even create the datasource with your custom algorithm to read the configuration but that’s another story.
Side note: this include pooling support either with tomcat-jdbc or commons-dbcp. All is configurable but here I used defaults for lisibility.
Now how to handle our different drivers? Just add a classpath to the resource:
<Resource id="ds" type="DataSource" classpath="mvn:com.oracle:ojdbc6:11.2.0.2.0"> ... </Resource>
I am a bit lazy so I used maven coordinates but you can use paths too.
TomEE will then force to use this particular driver for this datasource. Then simply use it in your application without caring at all about the driver.
Where it is really awesome is the fact you can dev an app without caring about datasources (if you use JPA particularly) and only prod guys configure it with prod details such as driver, pooling, validation, password, connection etc…
Reblogged this on Dinesh Ram Kali..