Results 1 to 2 of 2

Thread: RowMapper/ParameterizedRowMapper thread safety

  1. #1
    Join Date
    Mar 2011
    Posts
    2

    Default RowMapper/ParameterizedRowMapper thread safety

    Hi,

    Is it safe in a web based app (thread per request) to have static RowMappers mapping the resultset to a pojo?

    e.g. within a class you have code such as..

    Code:
    // Declare a rowmapper
    private static final CommentRowMapper commentRowMapper = new CommentRowMapper();
    
    // sample code where the row mapper is being used
    List<Comment> foundComments =  getSimpleJdbcTemplate().query(SELECT_COMMENTS_BY_REQUESTID_SQL,  commentRowMapper, requestId);
    
    // row mapper defined within a top-level class
    private static final class CommentRowMapper implements ParameterizedRowMapper<Comment> {
        public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
            Comment commentRow = new Comment();
            commentRow.setCommentText(rs.getString(Comment.COMMENT_TX));
            commentRow.setCreatedByUserId(rs.getString(Comment.CREATED_BY_USER_CD));
            return commentRow;
        }
    }
    Can I rely entirely on getSimpleJdbcTemplate() being thread-safe or is there an issue with this pattern of usage?

    Cheers for your help.. :-)
    Last edited by fewtrees; Mar 2nd, 2011 at 04:03 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Please use [ code][/code ] tags when posting code.

    As long as your class is thread safe there is no issue. JdbcTemplate is thread safe by design..
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •