Results 1 to 5 of 5

Thread: Sample code is multi-thread safe?

  1. #1
    Join Date
    Aug 2004
    Location
    Madrid, Spain
    Posts
    25

    Default Sample code is multi-thread safe?

    Hi,

    Taking a look to the sample code of section 10.2.5 of the reference documentation (Data Access using JDBC) I am left wondering whether it is multi-thread safe, since several methods share the same JdbcTemplate member variable.

    Regards,
    F.

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    A JdbcTemplate is threadsafe.

  3. #3
    Join Date
    Aug 2004
    Location
    Madrid, Spain
    Posts
    25

    Default Sample code is multi-thread safe?

    Rod,

    Yes I know you mention JdbcTemplate is thread-safe in both documentation and your books (at least the first one, I recently bought the second one but still waiting for delivery, so don't know for sure).

    I've read the source code of JdbcTemplate and now I think I understand why the sample code is thread safe. However I still feel like the sample code is a little bit misguiding for beginners. For instance, consider how such a programmer could "extend" the code:

    Code:
    public int getCount() {
        jt = new JdbcTemplate(dataSource);
        jt.setFetchSize(1);
        int count = jt.queryForInt("select count(*) from mytable");
        return count;
    }
    
    public List getList() {
        jt = new JdbcTemplate(dataSource);
        jt.setFetchSize(1000);
        List rows = jt.queryForList("select * from mytable");
        return rows;
    }
    Now the above code may still function but there would be no guarantees as to what fetch size will be used in queryForInt() and queryForList() in a multi-threaded scenario.

    Since changing the above code to use:
    Code:
        JdbcTemplate jt = new JdbcTemplate(dataSource);
        ...
    would solve the issue, my question is what does it buy to have jt as a member variable? Please forgive if I am clueless. I am just trying to understand.

    Thank you in advance,
    F.

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Well, the cheap answer is that the fetch size should not be set after initial construction and configuration of the jdbcTemplate, if the template is going to be shared... The fetch size would be configured a deployment setting, something set in the application context.

    At this point it is quite valid to construct a new JdbcTemplate for each use (feeding it a DataSource). It used to be that a lookup of database type and error codes would be done on each construction, a somewhat expensive process, but this information is now cached.

    So you can use either approach...

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  5. #5
    Join Date
    Aug 2004
    Location
    Madrid, Spain
    Posts
    25

    Default Thanks

    Thank you very much guys, now it is clear .

    It is interesting to see how some framework usage idioms develop.

    F.

Similar Threads

  1. Replies: 13
    Last Post: Oct 24th, 2007, 10:55 AM
  2. Replies: 4
    Last Post: Jun 20th, 2007, 11:06 AM
  3. Spring code remarks
    By Alarmnummer in forum Architecture
    Replies: 18
    Last Post: Apr 7th, 2005, 07:17 AM
  4. Sessions closing after commit
    By bendg25 in forum Data
    Replies: 0
    Last Post: Mar 21st, 2005, 04:38 AM
  5. Replies: 2
    Last Post: Jan 6th, 2005, 02:49 AM

Posting Permissions

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