Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jdbcTemplate.update freezes

I'm using a Spring JdbcTemplate without a "transactionManager" since I have mostly select to do.

When I try to call select queries from JUnit, it works, but when I try to call an "update", it freezes the test (no connection timeout, nothing, just waiting).

I've seen examples of jdbcTemplates insert/update without any transactionManager, but could it be the problem here ?

  public void insert(String param1, String param2) {

    String sql = "UPDATE MYTABLE SET name = :param1 where first_name = :param2";

    NamedParameterJdbcTemplate npJdbcTemplate = new NamedParameterJdbcTemplate(jdbcTemplate);
    SqlParameterSource namedParameters = new MapSqlParameterSource().addValue("param1", param1).addValue("param2", param2);

    npJdbcTemplate.update(sql, namedParameters);
  }
like image 514
Tristan Avatar asked Oct 23 '18 17:10

Tristan


People also ask

What is jdbctemplate update() method in Java?

Overview 1 JdbcTemplate is class which will help us to query the database 2 update () is a method provided by JdbcTemplate, which is used to update the java object into the database 3 update () takes 2 parameters, sql_query and parameters

What is spring jdbctemplate?

Overview Spring JdbcTemplate is a powerful tool for developers to focus on writing SQL queries and extracting results. It connects to the back-end database and executes SQL queries directly.

How do I use jdbctemplate in unit testing?

Also, we use JdbcTemplate in an example method getCountOfEmployees (). There are two ways to unit test methods that use JdbcTemplate. We can use an in-memory database such as the H2 database as the data source for testing.

How do I mock the jdbctemplate object?

We can mock the JdbcTemplate object so that we don't need to run the SQL statement on a database: In this unit test, we first declare a mock JdbcTemplate object with the @Mock annotation. Then we inject it to the EmployeeDAO object using ReflectionTestUtils . Also, we use the Mockito utility to mock the return result of the JdbcTemplate query.


1 Answers

The problem here was I had passed the same update query on the same line in a SQL client (Oracle SQL developer) but it had not been committed in this client.

My JUnit had been stalled for 12 hours and right after I commit the query in SQL developer, the update occurred in the JUnit.

It had nothing to do with transaction management in the app or autocommit status of the datasource.

like image 124
Tristan Avatar answered Oct 15 '22 11:10

Tristan