Setting Up a Data Source in Payara Micro

Uncategorized

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:

<data-source>
<name>java:global/ExampleDataSource</name>
<class-name>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</class-name>
<server-name>localhost</server-name>
<port-number>3306</port-number>
<database-name>mysql</database-name>
<user>root</user>
<password>root</password>
<!-- Example of how to use a Payara specific custom connection pool setting -->
<property>
<name>fish.payara.sql-trace-listeners</name>
<value>com.sun.gjc.util.SQLTraceLogger</value>
</property>
</data-source>

The class name is the name of the data source class for your database.

For Oracle DB:

<class-name>oracle.jdbc.pool.OracleDataSource</class-name>

For PostgreSQL:

<class-name>org.postgresql.ds.PGSimpleDataSource</class-name>

 

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:

<server-name>${server.name}</server-name>

 

These can be supplied to Payara Micro when starting it as -Dserver.name=localhost.

 

To use it in your code just use the following:

@Resource(name=”java:global/ExampleDataSource”)

DataSource ds;

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

{{cta(‘4129273b-d6b4-4ef3-8fb7-412a2d0dfdfb’)}}

 

 

Comments (6)

Post a comment

Your email address will not be published. Required fields are marked *

Payara needs the contact information you provide to us to contact you about our products and services. You may unsubscribe from these communications at any time. For information on how to unsubscribe, as well as our privacy practices and commitment to protecting your privacy, please review our Legal & Privacy Policy.

  1. Daniel Wamara

    The link to Github is a bit incorrect, the right one would be https://github.com/payara/Payara-Examples/tree/master/payara-micro/datasource-example (notice there are no capital letters in “payara-micro”)
    I prefer to configure a datasource and configure a persistence unit in the persistence.xml so that I won’t care about creating connections and adding more code because of it.

  2. Laurent DUVERGÉ

    Why aren’t there any persistence.xml file? I want my data not to be coupled with my webApp. I want to have a persistence.xml in a separate jar and my webApp to make calls to this jar to get the data. Can you please explain or give an example? Thanks

    1. Jonathan Coustick

      If you wish to have a persistence.xml then you can have one, and have in the tag the JNDI of the resource defined in the web.xml as in this example, but the persistence must be in the same WAR as Payara Micro does not support other formats.

  3. Guillermo

    I’ve tested up this example using postgresql and it did not work. It Looks like to be an known issue https://github.com/payara/Payara/issues/1584.

    1. Jonathan Coustick

      I am sorry but I have not been able to reproduce that error. What version of Payara Micro are you using?

  4. Bhupi

    I am doing a simple database example with postgres datasource & it failing in payara micro at step:

    code – Connection conn = ds.getConnection();

    Earlier, DS is injected from @Resource i.e.

    code – @Resource(name=”java:global/esWf”)
    code – DataSource ds;

    Database connection is working in test java code using:

    code – Connection connection
    = DriverManager.getConnection(“jdbc:postgresql://localhost:5432/postgres”, “user”, “pass@123”)

    Also, payara REST is working without the database piece.

    Datasource thing is new for me, and I might be missing out some minor step.

    I have referred the git examples and researched several related blogs, but not able to solve this.

    Here is the src and web folders: https://drive.google.com/file/d/1b3F74D1cby0sXuRhMqs9jjrp4e7lqbed/view?usp=sharing

    Please see if you can check what might be issue.

    Below is the null pointer error: –
    java.lang.NullPointerException
    at es.dms.DmsSrv.test(DmsSrv.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:176)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:253)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1636)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:259)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:757)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:577)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:158)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:520)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:217)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:95)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:524)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:33)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
    at java.lang.Thread.run(Thread.java:748)

Related Posts

4 minutes
Uncategorized

Leading the Way: Payara Platform Community 7 Beta Now Fully Jakarta EE 11 Certified

We’re excited to announce that Payara Platform Community 7 Beta application server is now fully certified as Jakarta EE 11 […]

Blue background with coral and fish. Left text: 'MONTHLY CATCH'. Right: laptop screen with tech tabs and Payara Community logo. 4 minutes
Community

The Payara Monthly Catch – August 2025

Welcome aboard the August 2025 issue of The Payara Monthly Catch! With summer in full swing, things may have felt […]

What Is a Java Application Server? A Short Guide 6 minutes
Jakarta EE

What Is a Java Application Server? A Short Guide

Enterprise Java applications power global commerce, healthcare, government and countless other industries. These systems must be scalable, secure and […]