What is the "default datasource" in the Payara Starter example of Payara Micro Database Authentication?

I think Payara Starter is a very good tool for getting started with Payara Micro.

I have one question. I selected “Payara Micro” Platform and “Form Authentication - Database” with Payara Starter. In this example, a table was created in the database and authentication worked!

However, I did not start the database myself. In the example, there are connection settings to the database in secured/DatabaseSetup.java. There, a DataSource object is injected with the following code:

@Resource
private DataSource dataSource;  // default datasource

It seems that jdbc/__default is being referenced, but I don’t understand what this means. Does Payara Micro have a built-in database?

I found it myself. Thank you. This is an embedded H2 database, right?

Yes, you are correct. By default, java:comp/DefaultDataSource (which corresponds to jdbc/__default) is used in the Payara Micro example, and this JNDI name is mapped to an H2 database pool named H2Pool. This is a built-in, in-memory H2 database that Payara Micro uses by default for demonstration and development purposes.

If you want to use a custom datasource, you can define your own JNDI name and configure it accordingly. For example:

  1. Define a Custom Datasource:
@DatabaseIdentityStoreDefinition(
    dataSourceLookup = "<CUSTOM-JNDI-NAME-HERE>",
    callerQuery = "select password from caller where name = ?",
    groupsQuery = "select group_name from caller_groups where caller_name = ?",
    hashAlgorithm = Pbkdf2PasswordHash.class
)
  1. Inject the Custom Datasource:
@Resource(name="<CUSTOM-JNDI-NAME-HERE>")
private DataSource dataSource;

In this setup, replace <CUSTOM-JNDI-NAME-HERE> with the JNDI name of your custom datasource. Ensure that the datasource is correctly configured in your domain.xmlfile.

By using a custom datasource, you can connect to external databases like MySQL, PostgreSQL, or any other JDBC-compliant database, thereby enhancing the flexibility and scalability of your application.

1 Like

Thank you for the detailed explanation!

This is a built-in, in-memory H2 database that Payara Micro uses by default for demonstration and development purposes.

Currently, it appears that the H2 database in Payara Micro’s default behavior operates in embedded mode (jdbc:h2:<path><databaseName>) instead of in-memory (jdbc:h2:mem:).

For example, if the Micro Runtime directory is C:\Temp\payaramicro-rt15778861792591636092tmp, the H2 database URL is jdbc:h2:C:\Temp\payaramicro-rt15778861792591636092tmp\lib\databases\embedded_default.

I have also confirmed that embedded_default.mv.db and embedded_default.lock.db are created in this location. Is this unintentional behavior?

Your observation is correct regarding Payara Micro’s configuration of the H2 database. Payara Micro uses an embedded H2 database, which persists data to disk by default.

Here is the relevant configuration from domain.xml for both Payara Server and Payara Micro:

Payara Server domain.xml:

<jdbc-resource pool-name="H2Pool" jndi-name="jdbc/__default" object-type="system-all" />
<jdbc-connection-pool is-isolation-level-guaranteed="false" name="H2Pool" datasource-classname="org.h2.jdbcx.JdbcDataSource" res-type="javax.sql.DataSource">
  <property name="URL" value="jdbc:h2:${com.sun.aas.instanceRoot}/lib/databases/embedded_default;AUTO_SERVER=TRUE" />
</jdbc-connection-pool>

Payara Micro domain.xml:

<jdbc-resource pool-name="H2Pool" jndi-name="jdbc/__default" object-type="system-all" />
<jdbc-connection-pool is-isolation-level-guaranteed="false" name="H2Pool" datasource-classname="org.h2.jdbcx.JdbcDataSource" res-type="javax.sql.DataSource">
  <property name="URL" value="jdbc:h2:${com.sun.aas.instanceRoot}/lib/databases/embedded_default;AUTO_SERVER=TRUE" />
</jdbc-connection-pool>
1 Like