title: Is Spring JdbcTemplate thread safe?
body: <p>I'm using Spring JdbcTemplate on one of my projects and now, when there are really very much requests with it - I started to face this exception:</p>
  
  <pre><code>org.springframework.jdbc.UncategorizedSQLException: CallableStatementCallback; 
  uncategorized SQLException for SQL [{? = call API.get_data_for_nb(?, ?)}]; 
  SQL state [99999]; error code [17009]; Closed Statement; 
  nested exception is java.sql.SQLException: Closed Statement
  </code></pre>
  
  <p>So the Closed Statement exception is received when you try to execute statement that is already closed, but in my case I don't close it by myself - I use JdbcTemplate exactly for that. So, firstly, what could be the reason for that?</p>
  
  <p>The JdbcTemplate object itself is contained in <code>@Stateless</code> EJB in this way:</p>
  
  <pre><code>@Stateless(name = "NbEdwServiceEJB")
  public class NbEdwServiceBean implements NbEdwServiceLocal, NbEdwServiceRemote {
  
      @Resource(mappedName = JNDI)    
      private DataSource dataSource;
  
      private static volatile JdbcTemplate jdbcTemplate;
  
      @PostConstruct
      protected void construct() {
          synchronized (NbEdwServiceBean.class) {
              if (jdbcTemplate == null) {
                  jdbcTemplate = new JdbcTemplate(dataSource);
                  jdbcTemplate.setResultsMapCaseInsensitive(true);
              }
          }
      }
  
      private String getDataFromDB(final String request, final int isDigitalSignVerified) {
          String response = null;
          try {
              response = jdbcTemplate.execute(SQL_GET_DATA, new CallableStatementCallback&lt;String&gt;() {
                  public String doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException {
                      cs.registerOutParameter(1, Types.VARCHAR);
                      cs.setInt(2, isDigitalSignVerified);
                      cs.setString(3, request);
                      cs.executeUpdate();
                      return cs.getString(1);
                  }
              });
          } catch (DataAccessException ex) {
              LOGGER.error("getDataFromDB()", ex);
          }
  
          return response;
      }
  }
  </code></pre>
  
  <p>I know that this is maybe not the strictly right way to do it, I could just create instance of JdbcTemplate for every stateless bean - so I might do just that. So, secondly, why is this ever happening? My suppose was that JdbcTemplate's execute method isn't thread safe, but can someone give the full explanation on what is going on?</p>
  
  <p>I have JEE version 5 running on WebLogic 10.3.5 if it's matter.        </p>