При запуске Hibernate я меняю пароль своей базы данных на неверный пароль, чтобы увидеть, как работает моя система.
Я обнаружил, что мой блок catch не ломает Hibernate init error, когда пароль неверен и не вызывает никаких ошибок. Другими словами, configure.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)
....
На трассе стека есть класс, который скрывает исключение.
Если вы посмотрите на метод public void execute(Target target)
из org.hibernate.tool.hbm2ddl.SchemaUpdate
. connectionHelper.getConnection();
находится на вложенных блоках catch try. Вызов этого метода выдает исключение SqlException, которое является catch в первом блоке 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);
}
}
}
Надеюсь это поможет,
<property name="hibernate.hbm2ddl.auto">update</property>
изhibernate.cfg.xml
трассировка стекаThrowable
, но порог не попадает в блокThrowable
catch.