• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception handling issue

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All ,

In my application , login to database is written in dao class . If user enters wrong credentials , it has to be caught in SQLException cacth block . In my application it is not coming to catch block . In the console it is showing the exception .

Do any Spring inbuilt functions are caching ?? if so , how to overcome that . I want to show the error message to user in same page.

Please help me on this ..
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gireesh Mulage wrote:Dear All ,

In my application , login to database is written in dao class . If user enters wrong credentials , it has to be caught in SQLException cacth block . In my application it is not coming to catch block . In the console it is showing the exception .

Do any Spring inbuilt functions are caching ?? if so , how to overcome that . I want to show the error message to user in same page.

Please help me on this ..



Are you using a Spring class/configuration to simplify your Data Access code. For instance, the JdbcTemplate catches SQLExceptions and converts them to DataAccessException instead.

try catching DataAccessException and see if that works for you.

Mark
 
Gireesh Mulage
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply ...

I am not using any Springs inbuilt functions for database connectivity . I am using Hibernate .

It is not catching in DataAccessException catch block also . how to overcome on this ?
 
Gireesh Mulage
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tryed using @ExceptionHandler :
@ExceptionHandler(SQLException.class)
public String handleSQLException(SQLException ex ,HttpServletRequest request) {

It is not entering into this block too ...


Also , in dispatcher-servlet.xml i have entered bean tag as follows :

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
<property name="exceptionMappings">
<props>
<prop key="java.sql.SQLException">error</prop>
</props>
</property>
</bean>


Please help me on this ..
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate does not throw SQLExceptions. It throws HibernateExceptions.

Mark
 
Gireesh Mulage
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Mark ,

As i mentioned while initializing topic , in our application , user needs to select to which database to connect (not at the time of starting server ). So , in that if user enters wrong username/pswd , then even though I am using Hibernate to connect to DB , it throws SQLException for that . problem ! .

How if I use Spring's Hibernate API ? Will that support my scenario ?

Thanks .
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to show/post the class/code so we can see what you mean. Otherwise, I am just guessing now.

Mark
 
Gireesh Mulage
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The code is as follows :

try{
Configuration conf=new Configuration();
Properties prop=new Properties();
String envarr[]=loginFormObj.getEnvironment().split("~~");
String dbinstance=envarr[0];
String host=envarr[1];
String port=envarr[2];

prop.setProperty(hib_driverClass, driverClass);
prop.setProperty(hib_url, "jdbc:oracle:thin:@"+host+":"+port+":"+dbinstance);
prop.setProperty(hib_userName, loginFormObj.getUserId());
prop.setProperty(hib_passWord, loginFormObj.getPassWord());

conf.setProperties(prop);
Session session=conf.configure().buildSessionFactory().openSession();
System.out.println("%%%%%%%%%%%%%%%");
}
catch(SQLException ex)
{
System.out.println("***in SQLException***");
}
catch(HibernateException hex)
{
System.out.println("***in hebernateException***");
}

Thanks.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the exception too.

Thanks

Mark
 
Gireesh Mulage
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Mark ..

The stacktrace is as follows :

May 2, 2011 2:31:54 PM org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=aiops001, password=****}
May 2, 2011 2:32:00 PM org.hibernate.cfg.SettingsFactory buildSettings
WARNING: Could not obtain connection metadata
java.sql.SQLException: ORA-01017: invalid username/password; logon denied

at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.O3log.receive2nd(O3log.java:496)
at oracle.jdbc.ttc7.TTC7Protocol.logon(TTC7Protocol.java:278)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:346)
at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:468)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:314)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:76)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2006)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1289)
at com.tcs.aiquery.dao.LoginHibDAO.connectToDB(LoginHibDAO.java:89)
at com.tcs.aiquery.client.LoginController.onSubmit(LoginController.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:626)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:150)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:354)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:342)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:763)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:709)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:613)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:536)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:595)
May 2, 2011 2:32:00 PM org.hibernate.dialect.Dialect <init>
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried stepping/debugging through that code to see exactly where the exception is thrown?

You are getting an SQLException, and looking at your code, it is possible that that is where it is being thrown, but it still feels like we are missing a piece of information. By stepping through code, you can see where it is being thrown and if it is even in that try portion of your code or elsewhere.

Are you using the LocalSessionFactoryBean in your Spring configuration to also create a sessionFactory?

Thanks

Mark
 
Gireesh Mulage
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Mark ,

Exception is being thrown while creating session : i.e,

Session session=conf.configure().buildSessionFactory().openSession();

I am not using LocalSessionFactoryBean in my application .

Dispatcher-servlet.xml has :


<context:component-scan base-package="com.XX.XX.client" />

<bean id="viewResolver" class=" org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Thank you .
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Dispatcher-servlet.xml would only contain beans on the web tier/layer. But something like your SessionFactory, DataSource etc would be defined in your middle tier/layer, and in a web application that middle tier would be loaded via Spring's ContextLoaderListener.

At this point, it looks like Hibernate is catching the SQLException and not bubbling it up. Although I would expect them to bubble it up as a HibernateException.

I don't think you will be able to change that. Unless there is more information about your setup/app that you aren't showing.

Thanks and good luck

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic