PostgreSQL Driver with Payara Micro

458 views
Skip to first unread message

Pedro Vieira Silva

unread,
Apr 28, 2017, 1:02:57 PM4/28/17
to Payara Forum
What is the correct approach for setting up the PostgreSQL driver in Payara Micro?

I currently have a maven project generating a WAR that has a declared dependency on PostgresSQL

    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.0.0</version>
    </dependency>

However when I start my application with the command:
java -jar payara-micro-4.1.1.171.1.jar --deploy payara-microservice-0.0.1-SNAPSHOT.war


I always get the exception:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.qualifier): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: The driver could not be loaded: org.postgresql.Driver
Error Code: 0
        at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:316)
        at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:147)
        at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
        at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:207)
        at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:760)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:265)
        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:731)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:205)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:183)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getDatabaseSession(EntityManagerFactoryImpl.java:528)
        at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactoryImpl(PersistenceProvider.java:385)
        at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactory(PersistenceProvider.java:313)
        at org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:207)
        at org.glassfish.persistence.jpa.PersistenceUnitLoader.<init>(PersistenceUnitLoader.java:114)
        at org.glassfish.persistence.jpa.JPADeployer$1.visitPUD(JPADeployer.java:223)
        at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
        at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:230)
        at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:926)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:435)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:220)
        at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:487)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Unknown Source)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Unknown Source)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
        at com.sun.enterprise.admin.cli.embeddable.DeployerImpl.deploy(DeployerImpl.java:134)
        at com.sun.enterprise.admin.cli.embeddable.DeployerImpl.deploy(DeployerImpl.java:149)
        at fish.payara.micro.impl.PayaraMicroImpl.deployAll(PayaraMicroImpl.java:1411)
        at fish.payara.micro.impl.PayaraMicroImpl.bootStrap(PayaraMicroImpl.java:1005)
        at fish.payara.micro.impl.PayaraMicroImpl.main(PayaraMicroImpl.java:201)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at fish.payara.micro.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at fish.payara.micro.boot.loader.Launcher.launch(Launcher.java:107)
        at fish.payara.micro.boot.loader.Launcher.launch(Launcher.java:70)
        at fish.payara.micro.boot.PayaraMicroLauncher.main(PayaraMicroLauncher.java:72)
        at fish.payara.micro.PayaraMicro.main(PayaraMicro.java:358)
Caused by: java.sql.SQLException: Error in allocating a connection. Cause: The driver could not be loaded: org.postgresql.Driver
        at com.sun.gjc.spi.base.AbstractDataSource.getConnection(AbstractDataSource.java:121)
        at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:135)
        ... 48 more
Caused by: javax.resource.spi.ResourceAllocationException: Error in allocating a connection. Cause: The driver could not be loaded: org.postgresql.Driver
        at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:319)
        at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:196)
        at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:171)
        at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:166)
        at com.sun.gjc.spi.base.AbstractDataSource.getConnection(AbstractDataSource.java:114)
        ... 49 more
Caused by: com.sun.appserv.connectors.internal.api.PoolingException: The driver could not be loaded: org.postgresql.Driver
        at com.sun.enterprise.resource.pool.datastructure.RWLockDataStructure.addResource(RWLockDataStructure.java:103)
        at com.sun.enterprise.resource.pool.ConnectionPool.addResource(ConnectionPool.java:282)
        at com.sun.enterprise.resource.pool.ConnectionPool.createResourceAndAddToPool(ConnectionPool.java:1512)
        at com.sun.enterprise.resource.pool.ConnectionPool.createResources(ConnectionPool.java:944)
        at com.sun.enterprise.resource.pool.ConnectionPool.initPool(ConnectionPool.java:230)
        at com.sun.enterprise.resource.pool.ConnectionPool.internalGetResource(ConnectionPool.java:511)
        at com.sun.enterprise.resource.pool.ConnectionPool.getResource(ConnectionPool.java:381)
        at com.sun.enterprise.resource.pool.PoolManagerImpl.getResourceFromPool(PoolManagerImpl.java:244)
        at com.sun.enterprise.resource.pool.PoolManagerImpl.getResource(PoolManagerImpl.java:170)
        at com.sun.enterprise.connectors.ConnectionManagerImpl.getResource(ConnectionManagerImpl.java:360)
        at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:307)
        ... 53 more
Caused by: com.sun.appserv.connectors.internal.api.PoolingException: The driver could not be loaded: org.postgresql.Driver
        at com.sun.enterprise.resource.pool.ConnectionPool.createSingleResource(ConnectionPool.java:924)
        at com.sun.enterprise.resource.pool.ConnectionPool.createResource(ConnectionPool.java:1189)
        at com.sun.enterprise.resource.pool.datastructure.RWLockDataStructure.addResource(RWLockDataStructure.java:98)
        ... 63 more
Caused by: com.sun.appserv.connectors.internal.api.PoolingException: The driver could not be loaded: org.postgresql.Driver
        at com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator.createResource(LocalTxConnectorAllocator.java:110)
        at com.sun.enterprise.resource.pool.ConnectionPool.createSingleResource(ConnectionPool.java:907)
        ... 65 more
Caused by: javax.resource.ResourceException: The driver could not be loaded: org.postgresql.Driver
        at com.sun.gjc.spi.DMManagedConnectionFactory.createManagedConnection(DMManagedConnectionFactory.java:117)
        at com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator.createResource(LocalTxConnectorAllocator.java:87)
        ... 66 more
]]


Even if I produce an Uber JarI get the same error.

I have verified that the PostgreSQL jar is being placed inside the War's WEB-INF\lib directory.

The only way I have managed to startup the application successfully is if I create a root dir with the driver jar inside the lib folder:

└───payara
    └───lib
            postgresql-42.0.0.jar




and start the application with following command:

java -jar payara-micro-4.1.1.171.1.jar --rootdir payara --deploy ../workspace/payara-microservice/target/payara-microservice-0.0.1-SNAPSHOT.war

So clearly this is something related with classpath loading, and payara micro not loading the content of the lib 

Steve Millidge

unread,
Apr 28, 2017, 1:12:16 PM4/28/17
to Payara Forum
The driver jar should be placed into your WEB-INF/lib directory. There is an example application in the Payara Examples which packages up a driver into a war file along with the application datasource definition in web.xml. Take a look at https://github.com/payara/Payara-Examples/tree/master/Payara-Micro/datasource-example which is for MySQL.

Pedro Vieira Silva

unread,
May 9, 2017, 12:20:17 PM5/9/17
to Payara Forum
Hi Steve,

Like I said in my original post it is being placed there, however for some reason it's not being loaded correctly, thus throwing that error.

This seems to be an issue with the payara server itself.

Pedro
Reply all
Reply to author
Forward
0 new messages