user1633277
user1633277

Reputation: 510

How to check if the connection established properly?

How do I check or know if the connection to remote database established properly , if not to switch another database.

private static SessionFactory configureSessionFactory(String name) throws HibernateException 
    {
        ServiceRegistry serviceRegistry;
        Configuration configuration = new Configuration();
        configuration.configure(name);
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        SessionFactory factory;
        factory = configuration.buildSessionFactory(serviceRegistry);
        return factory;
    }

is the method I am using to build my SessionFactory.

This is my config:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.url">
    jdbc:h2:tcp://193.168.1.70/history
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="PriceBar.hbm.xml"/>
</session-factory>
</hibernate-configuration>

I can connect to my DB , but how I determine if I am connected if the server goes down ?

Upvotes: 0

Views: 8571

Answers (2)

Farm
Farm

Reputation: 3396

Session session = factory.openSession();
session.doWork(new Work() {
    @Override
    public void execute(Connection connection) throws SQLException {
        //You got a connection
    }
});

Upvotes: 1

cnZach
cnZach

Reputation: 81

There is a connection level method isValid() you can try.

Upvotes: 0

Related Questions