-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Exceptions not thrown for invalid connection properties
PostPosted: Fri Nov 10, 2006 7:52 pm 
Newbie

Joined: Fri Nov 10, 2006 7:26 pm
Posts: 11
I'm having problems with Hibernate not throwning exceptions when I give it invalid database configuration information. I'm hoping to catch DB connection properties problems early, maybe when I create the session factory (e.g., throw an exception when Configuration.buildSessionFactory() is called). Unfortunately, no exception is thrown...

The Configuration.buildSessionFactory() dumps a stacktrace due to a SQLException, but it doesn't return any sort of exception....

Here are the details -- configuration, code, log4j output, etc. (Note i've removed the class mapping details).

Any ideas on how to get Hibernate to throw exceptions for DB connection properties problems?

Hibernate version:
3.1.3
Mapping documents:
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
      <property name="connection.driver_class">
         com.mysql.jdbc.Driver
         </property>
      <property name="connection.url">
         jdbc:mysql://localhost/xxxx
         </property>
      <property name="dialect">
         org.hibernate.dialect.MySQLInnoDBDialect
         </property>
        <property name="connection.username">xxx</property>
        <property name="connection.password">xxx</property>
        <property name="connection.pool_size">1</property>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

Code between sessionFactory.openSession() and session.close():
Code:
   try {
      Configuration hibernateConfig = new Configuration().configure();
      logg.debug(ME + ": after Configuration().configure()");
      if (null != sessionFactoryProperties) {
         hibernateConfig.addProperties(sessionFactoryProperties);
      }
      sessionFactory = hibernateConfig.buildSessionFactory();
      logg.debug(ME + ": after hibernateConfig.buildSessionFactory(), returned: "
            + ((null == sessionFactory) ? "NULL" : sessionFactory.toString()));
      try {
         SchemaValidator validator = new SchemaValidator(hibernateConfig);
         validator.validate();
         logg.debug(ME + ": after validator.validate()");
      }
      catch (HibernateException e1) {
         logg.warn(ME + ": Schema validation failed. Attempt to update schema.");
         try {
            SchemaUpdate updater = new SchemaUpdate(hibernateConfig);
            updater.execute(false, true);
         }
         catch (HibernateException e2) {
            logg.warn(ME + ": Schema update failed. Serious troubles ahead.");
         }
      }
   } catch (HibernateException e) {
      logg.error(ME + ": Failed to initialize Hibernate session factory w/"+e);
      sessionFactory = null;
   }

Name and version of the database you are using:
MySQL 4.1.11
Debug level Hibernate log excerpt:
Code:
     [java] DEBUG [main] HibernateUtil - getSessionFactory(): Configuring Hibernate Session Factory...
     [java] INFO  [main] Environment - Hibernate 3.1.3
     [java] INFO  [main] Environment - hibernate.properties not found
     [java] INFO  [main] Environment - using CGLIB reflection optimizer
     [java] INFO  [main] Environment - using JDK 1.4 java.sql.Timestamp handling
     [java] INFO  [main] Configuration - configuring from resource: /hibernate.cfg.xml
     [java] INFO  [main] Configuration - Configuration resource: /hibernate.cfg.xml
     [java] INFO  [main] Configuration - Configured SessionFactory: null
     [java] DEBUG [main] HibernateUtil - getSessionFactory(): after Configuration().configure()
     [java] INFO  [main] DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
     [java] INFO  [main] DriverManagerConnectionProvider - Hibernate connection pool size: 1
     [java] INFO  [main] DriverManagerConnectionProvider - autocommit mode: false
     [java] INFO  [main] DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:MySQL:///
     [java] INFO  [main] DriverManagerConnectionProvider - connection properties: {user=, password=****}
     [java] WARN  [main] SettingsFactory - Could not obtain connection metadata
     [java] java.sql.SQLException: Access denied for user ''@'localhost.localdomain' (using password: NO)
     [java]     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
     [java]     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:771)
     [java]     at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3649)
     [java]     at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1176)
     [java]     at com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
     [java]     at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
     [java]     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
     [java]     at java.sql.DriverManager.getConnection(DriverManager.java:525)
     [java]     at java.sql.DriverManager.getConnection(DriverManager.java:140)
     [java]     at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
     [java]     at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:72)
     [java]     at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1881)
     [java]     at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1174)
     [java]     at com.tm.proXDriver.model.util.HibernateUtil.getSessionFactory(HibernateUtil.java:73)
     [java]     at com.tm.proXDriver.model.util.HibernateUtil.getSession(HibernateUtil.java:148)
     [java]     at com.tm.proXDriver.model.AbstractDAO.<init>(AbstractDAO.java:38)
     [java]     at com.tm.proXDriver.model.LocationDAO.<init>(LocationDAO.java:30)
     [java]     at com.tm.proXDriver.bootstrap.ProcessBootstrap.openDBConnection(ProcessBootstrap.java:60)
     [java]     at com.tm.proXDriver.bootstrap.Bootstrap$1.widgetSelected(Bootstrap.java:90)
     [java]     at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:90)
     [java]     at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
     [java]     at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1085)
     [java]     at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3166)
     [java]     at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2842)
     [java]     at com.tm.proXDriver.bootstrap.Bootstrap.open(Bootstrap.java:68)
     [java]     at com.tm.proXDriver.bootstrap.Bootstrap.main(Bootstrap.java:53)
     [java] INFO  [main] Dialect - Using dialect: org.hibernate.dialect.MySQLInnoDBDialect
     [java] INFO  [main] TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
     [java] INFO  [main] TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
     [java] INFO  [main] SettingsFactory - Automatic flush during beforeCompletion(): disabled
     [java] INFO  [main] SettingsFactory - Automatic session close at end of transaction: disabled
     [java] INFO  [main] SettingsFactory - Scrollable result sets: disabled
     [java] INFO  [main] SettingsFactory - JDBC3 getGeneratedKeys(): disabled
     [java] INFO  [main] SettingsFactory - Connection release mode: auto
     [java] INFO  [main] SettingsFactory - Maximum outer join fetch depth: 2
     [java] INFO  [main] SettingsFactory - Default batch fetch size: 1
     [java] INFO  [main] SettingsFactory - Generate SQL with comments: disabled
     [java] INFO  [main] SettingsFactory - Order SQL updates by primary key: disabled
     [java] INFO  [main] SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
     [java] INFO  [main] ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
     [java] INFO  [main] SettingsFactory - Query language substitutions: {}
     [java] INFO  [main] SettingsFactory - Second-level cache: enabled
     [java] INFO  [main] SettingsFactory - Query cache: disabled
     [java] INFO  [main] SettingsFactory - Cache provider: org.hibernate.cache.NoCacheProvider
     [java] INFO  [main] SettingsFactory - Optimize cache for minimal puts: disabled
     [java] INFO  [main] SettingsFactory - Structured second-level cache entries: disabled
     [java] INFO  [main] SettingsFactory - Echoing all SQL to stdout
     [java] INFO  [main] SettingsFactory - Statistics: disabled
     [java] INFO  [main] SettingsFactory - Deleted entity synthetic identifier rollback: disabled
     [java] INFO  [main] SettingsFactory - Default entity-mode: pojo
     [java] INFO  [main] SessionFactoryImpl - building session factory
     [java] INFO  [main] SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
     [java] DEBUG [main] HibernateUtil - getSessionFactory(): after hibernateConfig.buildSessionFactory(), returned: org.hibernate.impl.SessionFactoryImpl@161dfb5


Top
 Profile  
 
 Post subject: Re: Exceptions not thrown for invalid connection properties
PostPosted: Mon Nov 13, 2006 2:12 pm 
Newbie

Joined: Fri Nov 10, 2006 7:26 pm
Posts: 11
I sort of achieved what I was after -- having Hibernate tell me (in some way) that my DB connection properties are invalid.

I was hoping that the Configuration.buildSessionFactory() would have barfed at me if the connection properties were not correct, especially since that method spews out tracebacks of that sort....

Unfortunately, this is not the case. It is not until I create a transaction and try to do something with that session do I get a 'org.hibernate.exception.GenericJDBCException: Cannot open connection' exception thrown. Well, thet's progress.


Top
 Profile  
 
 Post subject: Could not obtain connection metadata
PostPosted: Sat Dec 02, 2006 8:35 am 
Newbie

Joined: Sat Dec 02, 2006 8:17 am
Posts: 1
Location: Maryland
My project is having the exact same problem. The SettingsFactory
class is swallowing the exception on the first attempt to connect
to the database. See the buildSetting(buildSettings(Properties props)
method in the src and only logging a warning. In our case, we have
a two tier rich client and we'd like to let the user know immediately if
they entered an invalid username/password.

catch (SQLException sqle) {
log.warn("Could not obtain connection metadata", sqle);


Later on a GenericJDBCException is thrown that we can catch, but
we'd really like to catch it earlier in the process.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.