ajay516 wrote:
AFIK, buildSessionFactory doesn't try to connect to database. If there is connection failure, then you should expect that error to
arise when you run a query
It does try to connect. If the connection fails though, it tries to act like it never happened... The exception stack trace below is from the hibernate logs. the method buildSessionFactory does not throw an exception up to my program when this happens. I guess the general strategy is that I should not care about building the session factory, and only care about what happens with opened sessions.
Code:
INFO: Using Hibernate built-in connection pool (not for production use!)
Jan 22, 2007 1:26:36 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
Jan 22, 2007 1:26:36 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
Jan 22, 2007 1:26:36 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:3333/db1
Jan 22, 2007 1:26:36 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=postgres}
Jan 22, 2007 1:26:37 PM org.hibernate.cfg.SettingsFactory buildSettings
WARNING: Could not obtain connection metadata
org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:122)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:117)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc3.Jdbc3Connection.<init>(Jdbc3Connection.java:24)
at org.postgresql.Driver.connect(Driver.java:235)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:72)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:1881)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1174)
at com.rgdsft.hibernate.core.models.HibernateManager.connect(HibernateManager.java:284)