Openshift DataSource and TomEE
Openshift is a great, easy and free way to get a Java hosting nowadays. It can provide you a VM with Java 8, and with just one more click you will get MySQL or PostgreSQL setup as well.
Then to integrate it with your preferred server, you need to rely on environment variables. For MySQL, the tomee.xml TomEE configuration will look like:
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<Resource id="MySQL" aliases="jdbc/mypu1,jdbc/pu2" type="DataSource">
JdbcDriver = com.mysql.jdbc.Driver
JdbcUrl = jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/${OPENSHIFT_APP_NAME}
UserName = ${OPENSHIFT_MYSQL_DB_USERNAME}
Password = ${OPENSHIFT_MYSQL_DB_PASSWORD}
# standard rest of the configuration (eviction etc)
</Resource>
</tomee>
Before seeing that it is probably not the best way to do so for the coming TomEE 7.0.1, let see the features we rely on:
-
Aliases: allows to redefine a list (comma separated value) of names for this particular resource. This is great because if you have 2 wars with a persistence unit each but using the same MySQL database, then you can share the pool even if both persistence-unit don’t have the same jta-datasource value.
-
${X}: TomEE interpolates by default using system properties and environment variables.
TomEE 7.0.1 Openshift resources
TomEE comes with a feature called PropertiesResourceProvider. It is actually an interface that will provide, for a resource, the properties you will not need to declare in the resource.
That’s how the Openshift integration is done. Two implementations have been done, one for MySQL and one for PostgreSQL. This means that once the datasources is added to your application in openshift console, you can declare in your tomee the datasource with the following code:
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<Resource id="MySQL" aliases="jdbc/mypu1,jdbc/pu2" type="DataSource" properties-provider=”openshift:mysql”>
# standard rest of the configuration (eviction etc)
</Resource>
</tomee>
Minimallisticly you can make it this way:
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<Resource id="MySQL"
type="DataSource"
properties-provider=”openshift:mysql” />
</tomee>
But I would still encourage you to tune the size of the pool and the eviction (the provider will fill the validation query, so just set the eviction interval and that eviction is active - TestWhileIdle for instance):
<?xml version="1.0" encoding="UTF-8"?>
<tomee>
<Resource id="MySQL" properties-provider=”openshift:mysql” type="DataSource">
ValidationInterval = 30000
NumTestsPerEvictionRun = 5
TimeBetweenEvictionRuns = 30 seconds
TestWhileIdle = true
MaxActive = 200
</Resource>
</tomee>
Of course if you use PostgreSQL replace “openshift:mysql” by “openshift:postgresql”.
Conclusion
PropertiesResourceProvider is a great way to get rid of the boilerplate of the configuration of any application resources. This however doesn’t prevent you from adding configurations to resources for the part the provider doesn’t handle.
Note that one provider can be set globally in conf/system.properties and can allow to integrate with any enterprise configuration system to read the configuration from somewhere else. The application still has then the control on the import (or not) of the resources (not declaring a resource will not trigger any loading).
If you rely on Openshift (or Heroku which has its own provider) for more than one application, you will appreciate not having to remember how to configure your resources or having to ssh on the machines to check environment variables to setup new applications!
From the same author:
In the same category:

