Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting the database connection with spring and hibernate
    primarykey
    data
    text
    <p>I'm currently working on a java application. It's a standalone client with <code>Spring</code> and <code>Hibernate</code>. Also <code>C3P0</code>.<br /><br /> In the previous version we used a standard user(hardcoded in the configuration file) for the database connection but now we changed it so that every user has to provide his own credentials.<br /><br /> The beans with the code for the database are basically created on-demand.<br /> I changed the XML-files and added a postprocessor which sets the credentials as well as some connection settings. It looks similar to this now:</p> <pre><code>ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(); configurer = new PropertyPlaceholderConfigurer(); // properties are retrieved from a private method configurer.setProperties(getProperties()); context.addBeanFactoryPostProcessor(configurer); context.setConfigLocations(new String[] { "first.xml","second.xml" }); context.refresh(); return context.getBean("myClass", MyClass.class); </code></pre> <p>This all works as expected but now we reach the part where I'm currently stuck.<br /><br /> I want to provide a test functionality for the connection, so that the user can click a button and then is told if his credentials were accepted or not.<br /> My first idea was to do a simple select on the database. Sifting through the logs however, I noticed that Spring tries to connect to the database during the <code>refresh()</code> (or rather the instantiation of the bean) anyway. I can see exceptions in the logs, for example:<br/> <code>java.sql.SQLException: Connections could not be acquired from the underlying database!</code> <br /> Unfortunately, Spring doesn't seem to actually care. The exceptions are logged away but <code>refresh()</code> finishes and is not throwing any exceptions because of this. I had hoped that I could simply catch the exception and then I would know that the connection failed. <br /><br /> I could just do the select as planned, but I want to limit the connection attempts as much as possible, because the database server will block the user after several attempts. Even permanently if there are to many attempts(already had some fun with that, before I changed the settings for the connection pool).<br /><br /> My searches regarding this problem came up with practically nothing. Is there a way to get the exception somehow? Or does Spring provide an API of sorts that would tell me about the connection error during the instantiation/refresh?<br /><br /> Failing that, any ideas for an alternative approach? Preferably one that needs only a single attempt to determine if a connection is possible. </p> <p><strong>Edit:</strong> For anyone interested: I went with the suggestion from Santosh and implemented a connection test in <code>JDBC</code>.<br> Unfortunately there seems to be no easy way to make use of the database errors/exceptions encountered during the bean instantiation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload