• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Catch DuplicateKeyException Is recording an Error Even When Instruction Statment is Blank

 
Ranch Hand
Posts: 2004
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even with the catch DuplicateKeyException{} in the following code I still get a stack trace on the error log at this statement stmt.executeUpdate(sql);.
I thought if you did not have code between the brackets nothing happened.


 
Steve Dyke
Ranch Hand
Posts: 2004
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Might be helpful if we knew what the error was.



Underline code translates to line 32 in code I posted here.

[6/23/20 9:48:34:833 CDT] 0000008c SystemErr     R com.ibm.websphere.ce.cm.DuplicateKeyException: [SQL0803] Duplicate key value specified.
[6/23/20 9:48:34:835 CDT] 0000008c SystemErr     R at sun.reflect.GeneratedConstructorAccessor29.newInstance(Unknown Source)
[6/23/20 9:48:34:835 CDT] 0000008c SystemErr     R at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:58)
[6/23/20 9:48:34:835 CDT] 0000008c SystemErr     R at java.lang.reflect.Constructor.newInstance(Constructor.java:542)
[6/23/20 9:48:34:835 CDT] 0000008c SystemErr     R at com.ibm.websphere.rsadapter.GenericDataStoreHelper.mapExceptionHelper(GenericDataStoreHelper.java:627)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.ibm.websphere.rsadapter.GenericDataStoreHelper.mapException(GenericDataStoreHelper.java:686)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.ibm.ws.rsadapter.AdapterUtil.mapException(AdapterUtil.java:2267)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.ibm.ws.rsadapter.jdbc.WSJdbcUtil.mapException(WSJdbcUtil.java:1191)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.ibm.ws.rsadapter.jdbc.WSJdbcStatement.executeUpdate(WSJdbcStatement.java:1152)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.FAIWebApp._qualitycontrol.dimensionsheets.dimensionsheet.AddDimensionSheet.<init>(AddDimensionSheet.java:152)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.FAIWebApp._qualitycontrol.dimensionsheets.dimensionsheet.DimensionSheetDataConnectorV8.<init>(DimensionSheetDataConnectorV8.java:302)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.FAIWebApp._qualitycontrol.controllers.DimensionSheetV8ControllerServlet.doPost(DimensionSheetV8ControllerServlet.java:518)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at javax.servlet.http.HttpServlet.service(HttpServlet.java:595)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1232)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:781)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:480)
[6/23/20 9:48:34:836 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
[6/23/20 9:48:34:837 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:136)
[6/23/20 9:48:34:837 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:97)
[6/23/20 9:48:34:837 CDT] 0000008c SystemErr     R at com.FAIWebApp._application.qms.QMSFilterImproved.doFilter(QMSFilterImproved.java:73)
[6/23/20 9:48:34:837 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
[6/23/20 9:48:34:837 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
[6/23/20 9:48:34:837 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:967)
[6/23/20 9:48:34:837 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1107)
[6/23/20 9:48:34:837 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
[6/23/20 9:48:34:837 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:947)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:88)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:558)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java:608)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:985)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1074)
[6/23/20 9:48:34:839 CDT] 0000008c SystemErr     R at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1892)
 
Steve Dyke
Ranch Hand
Posts: 2004
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S/B line 36.
 
Saloon Keeper
Posts: 12027
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any mention of a DuplicateKeyException in the logs you posted.

Even so, you really shouldn't be using empty catch statements. They hide big problems that will bite you later.

What columns form unique keys of your table? Why are you choosing to ignore duplicates?

Your query is extremely vulnerable to SQL injection attacks by the way. Why did you ignore our previous advice to handle this issue?
 
Rancher
Posts: 4588
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the first line of the stack trace:
[6/23/20 9:48:34:833 CDT] 0000008c SystemErr     R com.ibm.websphere.ce.cm.DuplicateKeyException: [SQL0803] Duplicate key value specified.

Two things to check.
First, is the DuplicateKeyException you are catching the same as the one being thrown?  (I've made that mistake with other exceptions before which had matching names).
Second, is this code actually the deployed code?
Finally, you have a catch clause for Exception in that code, do those println statements appear in the logs?  If so, then this really isn't the code being executed.
 
Steve Dyke
Ranch Hand
Posts: 2004
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:It's the first line of the stack trace:
[6/23/20 9:48:34:833 CDT] 0000008c SystemErr     R com.ibm.websphere.ce.cm.DuplicateKeyException: [SQL0803] Duplicate key value specified.

Two things to check.
First, is the DuplicateKeyException you are catching the same as the one being thrown?  (I've made that mistake with other exceptions before which had matching names).
Second, is this code actually the deployed code?
Finally, you have a catch clause for Exception in that code, do those println statements appear in the logs?  If so, then this really isn't the code being executed.



To the best of my knowledge it is the correct DuplicateKeyException. Yes it is deployed. And no the println do not appear.
I am wandering if placing the try catch for DuplicateKeyException inside this code would be better?



Say like:



The reason for using the DuplicateKeyException in this manor is I run a loop which adds necessary data records based on dynamic criteria. I do not want to run overhead sql calls to see if the record exists before I try to insert it.
 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using exceptions for what should be normal control of program flow? Why have you got the empty catch in line 8?
 
Dave Tolls
Rancher
Posts: 4588
47
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Steve Dyke wrote:
To the best of my knowledge it is the correct DuplicateKeyException. Yes it is deployed. And no the println do not appear.



Then that code does not execute at all.
Seriously.
The only other option is that your version of the JVM is completely borked such that it does not operate within the rules of a JVM.

If that exception is being thrown from within that try block then, even if the first catch is using the wrong DuplicateKeyException the second catch (which is a catch all Exception catch) will catch it and print out those log lines.
That is assuming any println calls in your code appear in your logs.

Your original code above was this:


So either the exception is ignored, which seems to be what you want, or it is picked up by the next catch block.
That code will not result in a stack trace appearing in the logs for that exception, unless it is something inside the executeUpdate that is doing the logging, which would strike me as a little odd.

My next step would be either to debug it using the debugger, if this is easily reproducible in your test environment, or stick a load of debug statements in that area of code to track exactly what route it is taking and with what variables.
 
Steve Dyke
Ranch Hand
Posts: 2004
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why are you using exceptions for what should be normal control of program flow? Why have you got the empty catch in line 8?



I simply do not want DuplicateKeyException logged.

I know that this exception will be thrown. The only way to avoid it is to write more sql code to check for a record before the insert.
I want to avoid making two sql statements.
 
Steve Dyke
Ranch Hand
Posts: 2004
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
Your query is extremely vulnerable to SQL injection attacks by the way. Why did you ignore our previous advice to handle this issue?



Can you please remind me where I can see the previous advice?
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic