-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to test connection validity?
PostPosted: Thu Dec 03, 2009 6:19 pm 
Newbie

Joined: Thu Dec 03, 2009 6:04 pm
Posts: 1
Hi,
I have an application where the user can define connection parameters like server address, user name and password to the database.
But I'm not able to check if the connection succeeds.
I've written this code but although the connection parameters are invalid no exception is thrown.

Code:
try {
            Configuration config = new AnnotationConfiguration().configure();
            config.setProperty("hibernate.connection.url", jdbc);
            config.setProperty("hibernate.connection.username", user);
            config.setProperty("hibernate.connection.password", password);

            sessionFactory = config.buildSessionFactory();
            sessionFactory.openSession();
} catch(Throwable ex) {
            System.error.println("Error");
}


Is there any way how to check the connection before executing SQL query?

Thanks
Jaa


Top
 Profile  
 
 Post subject: Re: How to test connection validity?
PostPosted: Fri Dec 04, 2009 8:47 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
I think you can do this:
Code:
try {
            Configuration config = new AnnotationConfiguration().configure();
            config.setProperty("hibernate.connection.url", jdbc);
            config.setProperty("hibernate.connection.username", user);
            config.setProperty("hibernate.connection.password", password);

            sessionFactory = config.buildSessionFactory();
            // Added
            Session session = sessionFactory.openSession();
            Assert.assertFalse(sesssion.connection.isReadOnly());
} catch(Throwable ex) {
            System.error.println("Error");
}

I think then he'll connect to the database and check the metadata or something.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.