简体   繁体   中英

Hibernate: Connection to DB2 database fails (dialect?)

I am desperate trying to connect to a DB2 database via hibernate. Problem is here, I know, where the error comes from but I can't solve it or rather don't understand it. Maybe you can help me fix this problem. I would be very grateful!

The following code shows my hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
        <property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
        <property name="hibernate.connection.url">jdbc:db2://db2.example.com:50001/databasename</property>
        <property name="hibernate.connection.username">mydbuser</property>
        <property name="hibernate.connection.password">mydbpass</property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="show_sql">true</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <mapping class="media.definitions.Product" />

    </session-factory>
</hibernate-configuration>

This following code is a class which I've built by a tutorial:

package media.frontend;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");         
        StandardServiceRegistryBuilder sb = new StandardServiceRegistryBuilder();
        sb.applySettings(cfg.getProperties());
        StandardServiceRegistry standardServiceRegistry = sb.build();        
        sessionFactory = cfg.buildSessionFactory(standardServiceRegistry);
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

}

And the following error comes immediately when I start the program (which only tries to catch information of a single column). I've pasted the whole output:

Aug 14, 2014 5:45:45 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Aug 14, 2014 5:45:45 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Aug 14, 2014 5:45:45 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.ibm.db2.jcc.DB2Driver] at URL [jdbc:db2://anger.informatik.uni-leipzig.de:50001/PRAK14B]
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=dbprak12, password=****}
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 10 (min=1)
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure
WARN: HHH000341: Could not obtain connection metadata : DatabaseMetaData information is not known for server DB2/SUNX8664SQL10010 by this version of JDBC driver
Aug 14, 2014 5:45:45 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Exception in thread "main" java.lang.ExceptionInInitializerError
at media.frontend.HibernateTest.main(HibernateTest.java:15)
Caused by: java.lang.NullPointerException
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:244)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
at media.frontend.HibernateUtil.<clinit>(HibernateUtil.java:17)
... 1 more

Thanks in advance for your help!

Greets, Luke

I was facing similar issue in my code. Then i tried updating the recent versions of DB2 jars into my server and the code started working. Try to update your DB2 jar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM