简体   繁体   English

Spring JdbcTemplate和Threading

[英]Spring JdbcTemplate and Threading

Is it safe to fork off a Thread to execute an insert using a JdbcTemplate in Swing. 在Swing中使用JdbcTemplate执行插入是否安全。

It's a logging event and as much as possible I don't want it to affect perceived performance. 这是一个记录事件,我尽可能不希望它影响感知性能。

Also note that its thread-safety is very well explained in the Spring 3.1 reference documentation : 另请注意, Spring 3.1参考文档中对其线程安全性进行了很好的解释

13.2.1.2 JdbcTemplate best practices 13.2.1.2 JdbcTemplate最佳实践

Instances of the JdbcTemplate class are threadsafe once configured. 一旦配置,JdbcTemplate类的实例就是线程安全的。 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). 这很重要,因为这意味着您可以配置JdbcTemplate的单个实例,然后将此共享引用安全地注入多个DAO(或存储库)。 The JdbcTemplate is stateful, in that it maintains a reference to a DataSource, but this state is not conversational state. JdbcTemplate是有状态的,因为它维护对DataSource的引用,但此状态不是会话状态。

This answer from the Spring forum says yes.t=25965 来自Spring论坛的答案是yes.t = 25965

JdbcTemplate is a singleton that won't change state once it's set. JdbcTemplate是一个单例,一旦设置就不会改变状态。

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. 我没有直接从Swing使用JdbcTemplate ,但我有几个Web应用程序(每个请求都有一个单独的线程),我从来没有遇到任何线程问题。 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... 如果您担心线程问题,可以随时为每个日志记录线程创建一个新模板...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM