Mohamed
Mohamed

Reputation: 2352

NamedParameterJdbcTemplate very slow

I am using Spring NamedParameterJdbcTemplate in my search process, but when I get many results, its execution take several time and I don't know if there are a way to optimize it. Here my code:

 NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(dataSource);
 StringBuilder sb = new StringBuilder(query);
 query = sb.toString();
 return jt.query(query, map, new MyRowMapper());

Have you any idea please?

Thanks all!

Upvotes: 1

Views: 2754

Answers (1)

Ajinkya
Ajinkya

Reputation: 22710

Few things you can do
1. Why to create instance NamedParameterJdbcTemplate every time when method is invoked? Create it once and pass previously created instance to the method.

NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(dataSource);  

keep NamedParameterJdbcTemplate as class attribute and initialize it once.
2. What you are trying here?

 StringBuilder sb = new StringBuilder(query);
 query = sb.toString();  

Pass query directly to .query() method if its of type String.
3. Inside MyRowMapper, make sure you are fetching and assigning only required fields in mapRow method. This might be affecting the execution time as mapRow is called once for every record in resultset.

Upvotes: 1

Related Questions