3

Sorry to bother you but I keep getting an error et I don't really understand why.

As specified in the title I'm trying to connect on my postgresql database running heroku using hibernate. My app is also running on Heroku.

It's working in local mode, I've tried several postgre drivers.

Here's my hibernate.cfg.xml :

<!-- PostgreSQL -->

<property name="connection.url">jdbc:postgresql://paafeblgkhhbkx:PhZEtHl2RHwbWDQJJOurmej89-@ec2-54-243-243-252.compute-1.amazonaws.com:5432/d1vsgjgnfr1koq</property>

<property name="connection.username">paafeblgkhhbk</property>

<property name="connection.password">My password</property>

<property name="connection.driver_class">org.postgresql.Driver</property>

<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

<!-- Disable the second-level cache  -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>

The url, password and username are overridden when I get the session :

Configuration configuration = new Configuration();
configuration.addAnnotatedClass(Client.class);
// and other classes

try {  
    Class.forName("org.postgresql.Driver"); 
    System.out.println(" === === DRIVER FOUND === === ");   
} catch (ClassNotFoundException e) {
    e.printStackTrace();
    System.out.println(" === === DRIVER NOT FOUND === === ");
}
configuration.configure();
System.out.println("============================= EXPORT SCHEMA ==========================");
new SchemaExport(configuration).create(true, true);
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);

As you can see the driver is found. I'm pretty sure the url is ok too (If I try with another one I get a connection error)

Here my depedencies :

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.1.9.Final</version>
</dependency>
<dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.1-901.jdbc4</version>
</dependency>

And they are exported as stated here in my pom (I can see maven is copying it when I deploye my app) :

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
    <execution>
        <id>copy-dependencies</id>
        <phase>package</phase>
        <goals><goal>copy-dependencies</goal></goals>
    </execution>
</executions>
</plugin>

So at this point I can't see what I've done wrong :/

Here is the error log I'm getting :

Starting process with command java -cp target/classes:target/dependency/* test.Server

State changed from starting to up

org.hibernate.annotations.common.Version

INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}

org.hibernate.Version logVersion

INFO: HHH000412: Hibernate Core {4.1.9.Final}

org.hibernate.cfg.Environment

INFO: HHH000206: hibernate.properties not found

org.hibernate.cfg.Environment buildBytecodeProvider

INFO: HHH000021: Bytecode provider name : javassist

org.hibernate.cfg.Configuration configure

INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml

org.hibernate.cfg.Configuration getConfigurationInputStream

INFO: HHH000040: Configuration resource: /hibernate.cfg.xml

org.hibernate.cfg.Configuration doConfigure

INFO: HHH000041: Configured SessionFactory: null

=== === DRIVER FOUND === ===

============================= EXPORT SCHEMA ==========================

org.hibernate.dialect.Dialect

INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect

org.hibernate.tool.hbm2ddl.SchemaExport execute

INFO: HHH000227: Running hbm2ddl schema export

org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)

org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

INFO: HHH000115: Hibernate connection pool size: 2

org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

INFO: HHH000006: Autocommit mode: false

org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://paafeblgkhhbkx:PhZEtHl2RHwbWDQJJOurmej89-@ec2-54-243-243-252.compute-1.amazonaws.com:5432/d1vsgjgnfr1koq]

org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

INFO: HHH000046: Connection properties: {user=paafeblgkhhbkx, password=**}

org.hibernate.tool.hbm2ddl.SchemaExport execute

ERROR: HHH000231: Schema export unsuccessful

java.sql.SQLException: No suitable driver found for jdbc:postgresql://paafeblgkhhbkx:PhZEtHl2RHwbWDQJJOurmej89-@ec2-54-243-243-252.compute-1.amazonaws.com:5432/d1vsgjgnfr1koq

at java.sql.DriverManager.getConnection(DriverManager.java:640)

at java.sql.DriverManager.getConnection(DriverManager.java:169)

at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:193)

at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(ManagedProviderConnectionHelper.java:55)

at org.hibernate.tool.hbm2ddl.DatabaseExporter.(DatabaseExporter.java:52)

at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:367)

at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:304)

at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:293)

at persistance.SessionFactorySingleton.configureSessionFactory(SessionFactorySingleton.java:36)

at persistance.SessionFactorySingleton.getSessionFactory(SessionFactorySingleton.java:61)

at persistance.Controller.beginSession(Controller.java:24)

at persistance.Controller.sauvegarde(Controller.java:36)

at persistance.Controller.save(Controller.java:51)

at test.Server$1.handle(Server.java:44)

at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:83)

at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)

at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:86)

at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:589)

at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:83)

at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:561)

at sun.net.httpserver.ServerImpl$DefaultExecutor.execute(ServerImpl.java:137)

at sun.net.httpserver.ServerImpl$Dispatcher.handle(ServerImpl.java:367)

at sun.net.httpserver.ServerImpl$Dispatcher.run(ServerImpl.java:339)

at java.lang.Thread.run(Thread.java:679)

Thanks for your time !

JulienBe
  • 123
  • 3
  • 6

1 Answers1

7

There is something quite suspicious with your database URL:

jdbc:postgresql://paafeblgkhhbkx:PhZEtHl2RHwbWDQJJOurmej89-@ec2-54-243-243-252.compute-1.amazonaws.com:5432/d1vsgjgnfr1koq

Breakdown:

  • JDBC scheme: postgresql – OK
  • hostname: paafeblgkhhbkx – suspicious
  • port: PhZEtHl2RHwbWDQJJOurmej89-@ec2-54-243-243-252.compute-1.amazonaws.com – definitely wrong.

You are apparently trying to include username and password as a part of the connection URL, but that's not how it is done. These are configured using separate properties and if you are going to include them in the URI, this is how:

jdbc:postgresql://localhost/test?user=fred&password=secret


As a side note, I have successfully connected to your database so I can attest to the correctness of the credentials you have exposed to the public, in addition to the correct hostname. Time to change that password...

Chad Bingham
  • 32,650
  • 19
  • 86
  • 115
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Hey, thanks Marko, that's problem ! I knew I was a dumb question, now it's time to get some rest. Thanks again, I've tested and it's working just fine – JulienBe Feb 10 '13 at 15:29
  • 1
    I would suggest using env vars to store your db credentials. That way, if your db ever needs to be changed over, or your credentials changed, it will automatically be used in your app. A guide for Hibernate Spring is here: https://devcenter.heroku.com/articles/spring-mvc-hibernate – GregB Feb 10 '13 at 19:39
  • 1
    Also, you should rotate your db credentials immediately. Run `heroku pg:credentials --reset` right now. – GregB Feb 10 '13 at 19:41
  • @GregB I checked his database, it's empty :) – Marko Topolnik Feb 10 '13 at 22:24
  • Yes, don't worry about the credentials, it's just work for school, nothing very important :) But thanks for the warning – JulienBe Feb 12 '13 at 20:08
  • Not exactly sure how correct this is anymore but what would "test" represent in your url string correction? – Matt Jan 07 '22 at 03:08