Spring JdbcTemplate and Threading

こ雲淡風輕ζ 提交于 2019-12-18 05:15:15

问题


Is it safe to fork off a Thread to execute an insert using a JdbcTemplate in Swing.

It's a logging event and as much as possible I don't want it to affect perceived performance.


回答1:


This answer from the Spring forum says yes.t=25965

JdbcTemplate is a singleton that won't change state once it's set.




回答2:


Also note that its thread-safety is very well explained in the Spring 3.1 reference documentation:

13.2.1.2 JdbcTemplate best practices

Instances of the JdbcTemplate class are threadsafe once configured. This is important because it means that you can configure a single instance of a JdbcTemplate and then safely inject this shared reference into multiple DAOs (or repositories). The JdbcTemplate is stateful, in that it maintains a reference to a DataSource, but this state is not conversational state.




回答3:


Why would you think that it wouldn't be?

I've not used JdbcTemplate directly from Swing, but I have in several web applications (which have a separate thread per request) and I've never had any problems with threading issues from it. In this situation the template was configured once at application startup and repeatedly called with different parameters.

If you're worried about threading issues, you can always create a new template per logging thread...



来源:https://stackoverflow.com/questions/467324/spring-jdbctemplate-and-threading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!