views:

3007

answers:

4

Hi.

Does anybody know what's going on here:

I run hibernate 3.2.6 against a PostgreSQL 8.3 (installed via fink) database on my Mac OS X. The setup works fine when I use Java 6 and the JDBC 4 driver (postgresql-8.3-603.jdbc4). However, I need this stuff to work with Java 5 and (hence) JDBC 3 (postgresql-8.3-603.jdbc3). When I change the jar in the classpath and switch to Java 5 (I do this in eclipse), I get the following error:

Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
<Rows clipped for readability>
Caused by: java.sql.SQLException: No suitable driver
    at java.sql.DriverManager.getConnection(DriverManager.java:545)
    at java.sql.DriverManager.getConnection(DriverManager.java:140)
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423)

What's the problem here? I cannot see it. Here is my hibernate configuration:

<hibernate-configuration>

    <session-factory>
        <property name="connection.url">jdbc:postgresql:test</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">p</property>
        <property name="connection.pool_size">1</property>
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="show_sql">true</property>
        <mapping resource="com/mydomain/MyClass.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

EDIT: The longer, more usual form of the connection URL: jdbc:postgresql://localhost/test has the exact same behaviour.

The driver jar is definitely in the classpath, and I also do not manage to get any errors with this direct JDBC test code:

public static void main(String[] args) throws Exception {
  Class.forName("org.postgresql.Driver");  
  Connection con=DriverManager.getConnection("jdbc:postgresql://localhost/test","postgres", "p");
}
+1  A: 

did you notice that the connection url is incomplete

<property name="connection.url">jdbc:postgresql:test</property>

as opposed to

<property name="connection.url">jdbc:postgresql://localhost/test</property>
shyam
Yes, I actually had the longer version there first (same behaviour, works with Java 6/JDBC4 and doesn't with Java 5/JDBC3), I saw the shorter form which relies on defaults somewhere and left it there. I quess I'll change edit back for the question.
auramo
+1  A: 

You say "work with Java 5 and (hence) JDBC 3 (postgresql-8.3-603.jdbc3)." Maybe this is a faulty assumption.

The download page is confusing to me. It seems to imply that for Java 1.5 you need JDBC3 but it's not 100% clear. I'm not sure why the JDBC4 driver won't work with Java 1.5 (we use a DB2 JDBC4 driver with Java 1.5).

Have you tried the JDBC4 driver with Java 1.5?

Michael Sharek
I have, and it doesn't work. In your case JDBC4 driver for DB2 was probably compiled with 1.5 or earlier. In PostgreSQL's case it's compiled with 1.6 and therefore 1.5 cannot load the classes.
auramo
+2  A: 

I don't see you specifying the driver class in your Hibernate configuration. Try adding the following:

<hibernate-configuration>
    <session-factory>
        .
        .
        <property name="connection.driver_class">org.postgresql.Driver</property>
        .
    </session-factory>
</hibernate-configuration>
bmatthews68
Thanks! It worked. Somehow I had copied an example where the driver-class property was not present. And because it worked with JDBC4/Java6-setup I couldn't figure out that there could have been a problem with the config file.
auramo
+1  A: 

One of the new JDBC4 features is automatic loading via the Service Provider mechanism. By including a META-INF/services/java.sql.Driver file in the jar file, there is no longer the need to do Class.forName(""). This only works with the 1.6 JVM.

Kris Jurka