Results 1 to 5 of 5

Thread: Injecting JdbcTemplate instead of just DataSource?

  1. #1
    Join Date
    Oct 2004
    Posts
    9

    Default Injecting JdbcTemplate instead of just DataSource?

    Hi all,

    All the examples I have seen with regard to auto-wiring DAO classes have the DataSource being injected into the DAO instance via setter, and a JdbcTemplate created from the DataSource.

    I was thinking about injecting the JdbcTemplate directly rather than using the DataSource to create one, but was wondering if there was some drawback to this that has not occurred to me. The only thing I can think of is that it might make the DAO slightly less generic, menaing if I were to switch from JdbcTemplate to needing a HibernateTemplate or back to raw JDBC, I would need to change the Spring config file along with the code.

    No big deal, but it does save me a line of code when the JdbcTemplate is already there.

    Thanks,

    Art

  2. #2
    Join Date
    Aug 2004
    Location
    Toulouse, France
    Posts
    148

    Default

    personally, I don't find bad the injection of a *Template bean. It indeed raise the level of abstraction from a DataSource.
    As you mentionned, you have to change your config if you change the jdbc/hibernate/... way of doing. But you'd also have to change the DataSource if you were to store data in ldap instead of dbms. It is just a matter of abstraction level.

    Olivier

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

    Default

    I was thinking about injecting the JdbcTemplate directly rather than using the DataSource to create one, but was wondering if there was some drawback to this that has not occurred to me.
    I don't think so. It's a matter of taste. One advantage of injecting the JdbcTemplate directly is that things like NativeJdbcExtractors and custom exception translators can be reused easily. I inject JdbcTemplate sometimes when I need such customization.

    The only thing I can think of is that it might make the DAO slightly less generic, menaing if I were to switch from JdbcTemplate to needing a HibernateTemplate or back to raw JDBC, I would need to change the Spring config file along with the code
    I don't think this is a significant issue. The change is still localized to one class and it's config. You wouldn't really want to go back to raw JDBC, would you :?:

    Rgds
    Rod
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  4. #4
    Join Date
    Oct 2004
    Posts
    9

    Default

    You wouldn't really want to go back to raw JDBC, would you
    After using the JdbcTemplate and HibernateTemplate for a month or so now, I don't think I'll be going back to raw JDBC again. Just wanted to make sure I was not missing something with regard to injecting the Template.

    As a side note, Spring this has made a tremendous impact on the testability of my DAO classes. It really is a significant benefit to be able to run these outside of any container. Thanks Rod and team for the contribution (the book is great by the way).

  5. #5
    Join Date
    Oct 2004
    Location
    New York, NY
    Posts
    8

    Default Did I do anything wrong?

    Recently, I am having a case where I have to disable the XML sql error mapping, I am extending StoredProcedure and try to manually inject JDBCTemplate, but it's not working properly. Here is what I did:
    1. extend StoredProcedure, in the constructor, before compile(), create a new JDBCTemplate and set the SQLStateSQLExceptionTranslator, then set the new JDBCTemplate back.
    2. When I run it, the ExceptionTranslator is reset back to the default one -SQLErrorCodeSQLExceptionTranslator(the jdbcTemplate field is my class, but the exceptionTranslator field is SQLErrorCodeSQLExceptionTranslator class, not SQLStateSQLExceptionTranslator).
    However, if I just get the JDBCTemplate in the constructor and set the SQLStateSQLExceptionTranslator to it(before compile()), it works fine.

    I also noticed in the class JdbcDaoSupport, there is a JDBCTemplate. This JDBCTemplate can be injected without problems. However, since most of the lowlevel db ops are sealed by StoredProcedure and other classes, this JDBCTemplate is seldomly used. I checked the petclinic example and found that the JDBCTemplate in the JdbcDaoSupport subclasses is used only once for the identity key, which is sealed by stored procedures in my case. So I created another DAO support class without the JDBCTemplate at all.

    These are just my observation, I might miss/misunderstand something.

    On the other hand, spring + ibatis works so smoothly.

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. DataSource in JBoss with JdbcTemplate?
    By fabio.patricio in forum Data
    Replies: 1
    Last Post: Oct 10th, 2005, 10:29 AM
  3. Odd behaviour when injecting TransactionTemplate
    By damon311 in forum Container
    Replies: 3
    Last Post: Jul 23rd, 2005, 11:21 AM
  4. Ignoring missing Jndi DataSource within IDE?
    By Bill Pearce in forum Container
    Replies: 2
    Last Post: Oct 27th, 2004, 09:06 AM
  5. Help datasource not defined
    By jlm in forum Data
    Replies: 4
    Last Post: Aug 16th, 2004, 05:48 PM

Posting Permissions

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