Spring Boot MVC application with Postgresql deployment failed in Heroku - postgresql

I have a Spring Boot MVC project. It works fine with h2 database and also fine in local postgre database with following application.properties configuration
spring.datasource.url=myUrl
spring.datasource.username=myUsername
spring.datasource.password=myPassword
But when I am trying to deploy it in Heroku it is getting this following error:
Caused by: org.hibernate.HibernateException: Access to
DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
If I use this configuration
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=herokuPostgreSqlDbUrl
spring.datasource.username=herokuUsername
spring.datasource.password=mherokuPassword
I am getting this when try to run my project from STS and failed deployment.
Caused by: java.sql.SQLException: Driver:org.postgresql.Driver#17a3dff6
returned null for URL:myUrl
My pom is :
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
Can any body help ?

You need a proper JDBC url. See: https://springframework.guru/configuring-spring-boot-for-postgresql/

Related

Getting Error when trying to connect AWS Aurora Postgre cluster from spring boot app

I am trying to connect to AWS Aurora (Postgre) cluster from spring boot app using following properties
spring.datasource.driver.class.name = org.postgre.jdbc.Driver
spring.datasource.url=jdbc:postgresql://a2-xxxxx.cluster-xxxxxxxxxx.us-east-1.rds.amazonaws.com:5432
spring.datasource.username=username
spring.datasource.password=password
but I am getting following error -
*Caused by: java.lang.RuntimeException: Driver org.postgresql.Driver claims to not accept jdbcUrl, jdbc:postgresql://a2-xxxxx.cluster-xxxxxxxxxx.us-east-1.rds.amazonaws.com:5432
at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:110) ~[HikariCP-4.0.3.jar:na]
at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:331) ~[HikariCP-4.0.3.jar:na]
at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:114) ~[HikariCP-4.0.3.jar:na]*
Following are my maven pom.xml contents -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
You should read the jdbc documentation.
If the host is specified, the url needs a / at the end. If the database you are connecting to has a different name than the user, you also have to put the name of the database after the /. Even if they are the same, it's a good idea to include the db name because that default behavior is a bit confusing.

Unable to connect app to postgres server using yaml files

Following Error while connecting springboot app service to Postgres using yml.
NOTE: url: jdbc:postgresql://localhost/postgres
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#3f0846c6' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#3f0846c6': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1287)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:636)
... 62 common frames omitted
Caused by: java.net.UnknownHostException: db
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
at java.base/java.net.Socket.connect(Socket.java:591)
at org.postgresql.core.PGStream.<init>(PGStream.java:75)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
... 138 common frames omitted
Please suggest the alternatives and ideas
Well, you did not show your yaml file, that is most important. Error says problem with connection, so make sure your postgres is up and running, you can test it in console like
psql -h localhost -p port(default 5432) -U username -d database
spring:
datasource:
url: jdbc:postgresql://localhost:5432/databaseName?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false
username: yourUsername
password: yourPassword
jpa:
hibernate:
ddl-auto: update
show-sql: true
This is example of application.yaml for connecting to postgresql running on your machine, i am using docker for databases,so it works for that as well. Then make sure you have postgresql in your build file. If you use maven, it looks like this
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
And don`t forget JPA, which you probably have, as i see from error, but anyway
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Version is defined as parent. Spring boot makes configuration and connection for you, just by adding dependencies and reading your application.yml file.

What does "Unable to create o.a.tomcat.jdbc.pool.ConnectionPool" mean?

I get this error when running app:
main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create
initial connections of pool.
The first java error is here:
java.sql.SQLException: null
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:323)
~[tomcat-jdbc-8.5.16.jar:na]
I have added dependency to postgres in my pom:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
I dont really understand the error. What is wrong here?
Details:
Spring-boot app deployed to remote server.
Installed psql database
OS: Ubuntu 18.04
Here is postgres connection file:
spring.main.banner-mode=off
logging.level.org.springframework=ERROR
spring.jpa.hibernate.ddl-auto=none
spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=myuser
spring.datasource.password=mypass
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

Configure a db2 datasource with Thorntail / Wildfly Swarm

Has anyone managed to configure a db2 datasource with Thorntail / Wildfly Swarm?
As far as I understand: As soon as I pull in the datasources fraction, the db2 driver should be autodetected according to documentation (https://docs.thorntail.io/2.3.0.Final/#auto-detecting-jdbc-drivers_thorntail).
So the only thing I should have to do is reference "ibmdb2" as the driver-name in my datasource, right?
pom.xml (using Thorntail 2.3.0.Final)
<dependency>
<groupId>io.thorntail</groupId>
<artifactId>datasources</artifactId>
</dependency>
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc_license_cu</artifactId>
<version>10.1</version>
</dependency>
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc4</artifactId>
<version>4.22.29</version>
</dependency>
project-defauls.yml
swarm:
context:
path: /
datasources:
data-sources:
MYDS:
driver-name: ibmdb2
connection-url: jdbc:db2://host:port/schema
user-name: user
password: password
Currently I get the following error on startup:
2019-05-02 09:07:52,747 INFO [org.wildfly.swarm.datasources] (main) THORN1003: Auto-detected JDBC driver for ibmdb2
2019-05-02 09:07:57,660 ERROR [org.jboss.as.controller.management-operation] (ServerService Thread Pool -- 16) WFLYCTL0013: Operation ("add") failed - address: ([
("subsystem" => "datasources"),
("jdbc-driver" => "ibmdb2")
]) - failure description: "WFLYJCA0114: Failed to load datasource class: com.ibm.db2.jdbc.DB2XADataSource"
You found a bug in the JDBC driver autodetection code. The driver was (probably) autodetected, but it was wrongly configured. Specifically, this line of code sets the XA datasource class name to com.ibm.db2.jdbc.DB2XADataSource, which doesn't exist. (That's actually what your error message says, but I also confirmed it by looking into the JDBC driver JAR.) The correct class name is com.ibm.db2.jcc.DB2XADataSource. I filed THORN-2398 and submitted a PR with a fix.
I'm not sure if there's a simple workaround, because JDBC driver autodetection is performed after all configuration is applied. Perhaps the following hack might work. Define a new JDBC driver in project-defaults.yml like this:
thorntail:
datasources:
jdbc-drivers:
mydb2:
driver-module-name: com.ibm.db2jcc
driver-xa-datasource-class-name: com.ibm.db2.jcc.DB2XADataSource
But keep everything else intact. That means there will be 2 JDBC drivers for DB2, one autodetected (which will create the com.ibm.db2jcc module), and the second one you create that will piggyback on the infrastructure created by the first. If that works, just change driver-name: ibmdb2 in your data source to driver-name: mydb2.
If this doesn't work, you'll have to move off of JDBC driver autodetection for now, until the issue is fixed.

org.postgresql.util.PSQLException: Protocol error. Session setup failed

i am trying to connect to a postgresql server 9.3 running in my local machine.
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1100-jdbc41</version>
</dependency>
Also tried above jar for Driver class.
Caused by: org.postgresql.util.PSQLException: Protocol error. Session setup failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:510)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:173)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
resulted in above exception.
any idea how to ge around this problem?
the versions are correct. There was a typo in my connection string. the port given was wrong. After specifying the port, connection created successfully.