Having previously blogged about setting up data sources in Payara Server, we thought we should mention that you can also set up data sources in Payara Micro. In this blog, we'll take you through the process of setting up a data source using Payara Micro, including code snippets and where to find full code examples. Let's get started!
First, to use a database as a data source in Payara Micro, you'll need to have the database already running. Once you have that in place, download the JDBC driver for the database and put it into your WEB-INF/lib directory.
In your web.xml add the following:
<!-- Example of how to use a Payara specific custom connection pool setting -->
The class name is the name of the data source class for your database.
For Oracle DB:
The database name must also be changed to the name of the database you are using i.e. Oracle or PostgreSQL.
Edit the user, password, server name and port number to match your database installation.
Environment variables, system properties and aliases are supported in the web.xml, so for example, instead of specifying localhost as the server name you could do the following:
These can be supplied to Payara Micro when starting it as -Dserver.name=localhost.
To use it in your code just use the following:
A complete example is available on Github at: https://github.com/payara/Payara-Examples/tree/master/Payara-Micro/datasource-example
Why not give it a try? Payara Micro is the microservices-ready version of Payara Server. Less than 70MB in size, it requires no installation or configuration and no need for code rewrites – so you can build and deploy a fully working app within minutes.
Download Payara Micro