Multiple JdbcTemplate instances or not?

后端 未结 4 1721
一向
一向 2021-02-04 01:01

From what I understand, both DataSource and JdbcTemplates are threadsafe, so you can configure a single instance of a JdbcTempla

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 01:57

    IMO, there is no problem to inject JdbcTemplate to your (multiple) DAO(s). The template is used to "wire" your DAO to the physical resource (db connection) when you need to run db query. So if the SessionFactory and the TransactionManager are properly configured you will not run into concurrency problems - Spring manages the lifecycle of the beans you need for working with you persistence layer. The advantages of using a template are:

    1. JDBC template manages physical resources required to interact with the DB automatically, e.g. create and release the database connections.
    2. The Spring JDBC template converts the standard JDBC SQLExceptions into RuntimeExceptions. This allows you to react more flexible to the errors. The Spring JDBC template converts also the vendor specific error messages into better understandable error messages

提交回复
热议问题