(I'm using Spring 2.5.1.)
Seeing that a SimpleJdbcCall instance always uses the same parameter names (and types) and ParameterizedRowMapper, I started to declare each SimpleJdbcCall as instance variable in my DAO. In each DAO method, I would instantiate a new SimpleJdbcCall only if the instance is null:
Is this approach thread-safe?Code:public class JdbcCoursesDAO extends SimpleJdbcDaoSupport implements CoursesDAO { private SimpleJdbcCall sprocAddCourse; // Stored procedure name: public static final String SP_ADD_COURSE = "Courses_DB.sp_addCourse"; public Map addCourseDefinition(CourseCatalogRow row) throws SQLException { if (sprocAddCourse == null) { sprocAddCourse = new SimpleJdbcCall(getJdbcTemplate()) .withProcedureName(SP_ADD_COURSE) .withoutProcedureColumnMetaDataAccess() .useInParameterNames("course_name" , "cat_id" , "description" , "type_id" , "active") .declareParameters( new SqlParameter("course_name", Types.VARCHAR), new SqlParameter("description", Types.VARCHAR), new SqlParameter("cat_id", Types.NUMERIC), new SqlParameter("type_id", Types.NUMERIC), new SqlParameter("active", Types.NUMERIC), new SqlOutParameter("coursedef_id", Types.NUMERIC) ) .returningResultSet("RESULT_SET", new CourseCatalogRowMapper()); } SqlParameterSource in = new MapSqlParameterSource() .addValue("course_name", row.getCourse_name()) .addValue("cat_id", row.getCat_id()) .addValue("description", row.getDescription()) .addValue("type_id", row.getType_id()) .addValue("active", row.isActive()); Map out = sprocAddCourse.execute(in); return out; }
I'm thinking that since SqlParameterSource is local, that takes care of thread-safety. Is my thinking correct?
Thanks.


Reply With Quote