Luciano Fiandesio
Luciano Fiandesio

Reputation: 10215

Hikari + Hibernate + Postgres: can't connect

I have an application that uses C3p0 with Hibernate 5 and Hibernate. I would like to try out Hikari, but I'm unable to get the application to run.

Maven

  <dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>3.4.1</version>
  </dependency>

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

Hibernate version is: 5.2.17.Final

Spring configuration

public LocalSessionFactoryBean sessionFactory()
    throws Exception
{
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource( dataSource() );

    ...
}

private DataSource dataSource()
{
    String jdbcUrl = "jdbc:postgresql://localhost/demo";
    String user = "john";
    String pw = "pwd123";
    String url = String.format( "%s?user=%s&password=%s", jdbcUrl, user, pw);

    final HikariDataSource ds = new HikariDataSource();
    ds.setMaximumPoolSize( 10 );
    ds.setDataSourceClassName( "org.postgresql.ds.PGSimpleDataSource" );
    ds.addDataSourceProperty( "url", url );
    ...
    return ds;
}

I have tried different permutations of the above approach, including passing username and password in to the Datasource directly:

ds.addDataSourceProperty( "user", "john" );
ds.addDataSourceProperty( "password", "pw123" );

But I always end up with this error:

Caused by: java.sql.SQLFeatureNotSupportedException
   at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:135)

which is caused by this Hikari function:

@Override
public Connection getConnection(String username, String password) throws SQLException
{
   throw new SQLFeatureNotSupportedException();
}

Upvotes: 0

Views: 913

Answers (1)

user3487063
user3487063

Reputation: 3682

Look at this issue. It tells you have to either use jdbcUrl or dataSourceClassName but not both. I see you are using both, so just use one as mentioned in that issue and check if that helps.

Apart from that, I just checked when that getConnection(String username, String password) throws SQLException gets called, looks like if you have: hibernate.connection.username and/or hibernate.connection.password set, thats when it gets called. Make sure you don't have those two properties set somewhere.

Upvotes: 2

Related Questions