I was debugging a piece of code that threw a Java.lang.OutOfMemoryError. After a short investigation, I spotted that the SQL query the coded executed did not contain a ‘WHERE’ clause causing a nearly full download of a large table, making the process run out of memory.
The fix was simple and quick: add a WHERE clause to the SQL query. Then, as part of preventative maintenance, I thought to check why this query even executed as the parameters were present in the code, ready to be inserted into the SQL statement.
I want the NamedParameterJdbcTemplate to throw an exception like ‘Hey, you should have had two parameters in this query; you have none!’. So I asked ChatGPT for assistance: how can I check beforehand if all parameters provided found their way into the SQL inside the WHERE-clause?
How to check when you run jdbctemplate.query
that all NamedParameterSource
variables have been filled in?
This is what it came up with:
You can check if all
NamedParameterSource
variables have been filled in by using theSqlParameterSourceUtils
class provided by Spring framework. Here's an example:
1NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
2String sql = "SELECT * FROM my_table WHERE column1 = :param1 AND column2 = :param2";
3MapSqlParameterSource params = new MapSqlParameterSource();
4params.addValue("param1", "value1");
5//params.addValue("param2", "value2"); //Commented out to simulate a missing parameter
6
7SqlParameterSourceUtils.assertAllParamsSet(params, sql);
8List<MyObject> results = jdbcTemplate.query(sql, params, new MyRowMapper());
In this example, the
assertAllParamsSet
method will throw anIllegalArgumentException
if any named parameter in the SQL query is missing in theparams
object. This allows you to catch missing parameters before running the query. Note that this method only works forNamedParameterJdbcTemplate
andSqlParameterSource
objects, but you can adapt it to other types of JDBC templates and parameter sources if needed.
So the ‘money shot’ (Pardon my French) is at line 7. Since I’m looking for a way to check (assert) that all my parameters are used (set), reading this code suggestion made me a happy little nerd.
I quickly discovered that this API call, part of this helper class, does not exist; it never has. After the IDE gave all sorts of ‘not found’ indications, I decided to look up this part of the code, and this is what happened:
An old programmer joke is that you’re in trouble if there are 0 results when you Google your problem.
Yikes.
In the end, I needed to implement this myself rather than expecting the library to do it for me. ChatGPT suggested a helper class and API call that simply does not exist: it would be a nice addition to the Util, so I might do that.
ChatGPT dreamt of helping me; the bot hallucinated about APIs that would exist in an ideal world, just not in the real world.
Optimize with ZEN's Expertise
Upgrade your development process or let ZEN craft a subsystem that sets the standard.
Read more:
The AI Revolution: How Generative AI is Making SEO practices Obsolete!
Artificial Intelligence (AI) and Generative AI are leading a revolution that will obsolete traditional SEO practices lik...
Hallucinating AI - ChatGPT suggests APIs that never existed
I was debugging a piece of code that threw a Java.lang.OutOfMemoryError. After a short investigation, I spotted that the...
Guest trainer multi-cloud at Young Mavericks
As part of the Data Science trajectory at Young Mavericks, ZEN Software provided an introduction to Multi-Cloud. How can...
The $100 billion product demo failure: Google’s BARD
Since the launch of chatGPT, it’s been DEFCON 1 at Google HQ. ChatGPT could easily lead to a golden-egg-laying-goose kil...
'The End of Programming' is silly sales nonsense
With the recent rise of Github’s CoPilot and ChatGPT you can see the technology for code generation is ‘hot’. (Side note...
Remote Coding Job Interviews are DEAD because of Nvidia and ChatGPT
With the rise of remote work, remote coding job interviews have become increasingly popular. However, this shift has bro...