启动Hibernate时,我将数据库密码更改为错误的密码只是为了查看我的系统是如何工作的。
我发现我的catch块在密码错误时没有捕获Hibernate init错误并且没有抛出任何错误。换句话说,configuration.buildSessionFactory不会抛出错误。
try {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
// This not throw org.postgresql.util.PSQLException. Just print stack trace.
SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// And we'll NEVER come here from configuration.buildSessionFactory error.
}
堆栈跟踪:
ERROR: HHH000319: Could not get database metadata
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:291)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:106)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:123)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:28)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:20)
at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30)
at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:22)
at org.postgresql.Driver.makeConnection(Driver.java:391)
at org.postgresql.Driver.connect(Driver.java:265)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:193)
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:503)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
at cmabreu.infra.database.ConnFactory.getSession(ConnFactory.java:31)
at cmabreu.infra.repositorios.BasicRepository.<init>(BasicRepository.java:19)
at cmabreu.infra.repositorios.ConfigRepository.<init>(ConfigRepository.java:14)
at cmabreu.services.ConfigService.<init>(ConfigService.java:16)
at cmabreu.configurator.SingleConfig.getConfig(SingleConfig.java:21)
at cmabreu.action.IndexAction.execute(IndexAction.java:25)
....
答案 0 :(得分:2)
堆栈跟踪上有一个隐藏异常的类。
如果您查看来自public void execute(Target target)
的方法org.hibernate.tool.hbm2ddl.SchemaUpdate
。 connectionHelper.getConnection();
位于嵌套的try catch块上。对此方法的调用产生SqlException,它是第一个try / catch块中的catch,在catch中打印堆栈跟踪并且异常是进一步抛出,但这次是由第二个try / catch块捕获,它隐藏了异常。代码如下所示:
public void execute(Target target) {
LOG.runningHbm2ddlSchemaUpdate();
Connection connection = null;
Statement stmt = null;
Writer outputFileWriter = null;
exceptions.clear();
try {
DatabaseMetadata meta;
try {
LOG.fetchingDatabaseMetadata();
connectionHelper.prepare( true );
// THE SQLEXCEPTION IS THROWN <---------------
connection = connectionHelper.getConnection();
meta = new DatabaseMetadata( connection, dialect, configuration );
stmt = connection.createStatement();
}
catch ( SQLException sqle ) {
// THE SQLEXCEPTION IS CATCH <------------
exceptions.add( sqle );
// PRINTS STACK TRACE <-----------------
LOG.unableToGetDatabaseMetadata(sqle);
// THE EXCEPTION IS THROWN AGAIN <-----------------
throw sqle;
}
LOG.updatingSchema();
if ( outputFile != null ) {
LOG.writingGeneratedSchemaToFile( outputFile );
outputFileWriter = new FileWriter( outputFile );
}
List<SchemaUpdateScript> scripts = configuration.generateSchemaUpdateScriptList( dialect, meta );
for ( SchemaUpdateScript script : scripts ) {
String formatted = formatter.format( script.getScript() );
try {
if ( delimiter != null ) {
formatted += delimiter;
}
if ( target.doScript() ) {
System.out.println( formatted );
}
if ( outputFile != null ) {
outputFileWriter.write( formatted + "\n" );
}
if ( target.doExport() ) {
LOG.debug( script.getScript() );
stmt.executeUpdate( formatted );
}
}
catch ( SQLException e ) {
if (!script.isQuiet()) {
if ( haltOnError ) {
throw new JDBCException( "Error during DDL export", e );
}
exceptions.add( e );
LOG.unsuccessful(script.getScript());
LOG.error(e.getMessage());
}
}
}
LOG.schemaUpdateComplete();
}
catch ( Exception e ) {
// THE EXCEPTION IS CATCH HOWEVER THIS TIME IS NOT THROWN FURTHER exceptions.add( e );
LOG.unableToCompleteSchemaUpdate(e);
}
finally {
try {
if ( stmt != null ) {
stmt.close();
}
connectionHelper.release();
}
catch ( Exception e ) {
exceptions.add( e );
LOG.unableToCloseConnection(e);
}
try {
if( outputFileWriter != null ) {
outputFileWriter.close();
}
}
catch(Exception e) {
exceptions.add(e);
LOG.unableToCloseConnection(e);
}
}
}
希望这有帮助,