如何在 IoC 容器外进行简单的 Spring JDBC 事务?

发布于 2024-07-13 23:08:18 字数 549 浏览 14 评论 0原文

我正在从事的项目在其所有样板荣耀中使用直接 JDBC 数据访问,并且不使用任何事务。 我觉得使用事务和简化数据访问方法的编写方式很重要,特别是当前正在进行一些更改。 该项目已经存在相当长一段时间了,并且不适合 ORM 框架。 它还使用了大量的单例(呃),并且解开它以使其能够使用依赖项注入将是相当大量的工作,而且我认为我无法说服任何人我们现在应该这样做。

我喜欢 Spring JDBC 的接口,特别是通过它的 SimpleJdbcTemplate。 我的问题是如何为此启用一些简单的(每个 servlet 请求)事务,而无需在每个数据访问方法中以编程方式设置任何内容或使用 Spring IoC 容器或 AOP。 我尝试过自己的架构,最终得到一个类似于 SimpleJdbcTemplate 的接口,并且在请求上下文中调用时可以使用单个请求本地连接和事务(通过 ServletRequestListenerThreadLocal)。 它似乎工作得很好,但我认为使用像 Spring JDBC 这样好的外部库会更好。

有人对此有经验吗?

The project I'm working on uses straight JDBC data access in all its boilerplate glory and doesn't use any transactions. I feel like using transactions and simplifying the way data access methods are written is important, especially with some changes being made currently. The project has been around for quite a while and isn't suited to an ORM framework. It also uses lots of Singletons (ugh) and untangling it to make it able to use dependency injection would be a fair amount of work and I don't think I could convince anyone that we should do that now.

I like the interface of Spring JDBC, specifically through its SimpleJdbcTemplate. My question is about how to enable some simple (per servlet request) transactions for this, without having to set anything programmatically in every data access method or using the Spring IoC container or AOP. I've played around with my own architecture that ends up with an interface similar to SimpleJdbcTemplate's and can use a single request-local connection and transaction when calls to it are made in the context of a request (through a ServletRequestListener with a ThreadLocal). It seems to work well, but I think using a good external library like Spring JDBC would be preferable.

Anyone have any experience with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2024-07-20 23:08:18

Spring 以声明方式为您处理事务,而您不必担心编写 AOP 类。 如果您使用 JDK 5 或更高版本以及 Spring 2.5,则使用 注释

我不同意每个 servlet 事务。 您应该有一个服务层,它是应用程序中了解工作单元的部分。 控制器调用处理事务的服务。

Spring handles transactions for you declaratively without you having to worry about writing AOP classes. If you're using JDK 5 or higher and Spring 2.5, you have it even better with annotations.

I would disagree with per-servlet transactions. You should have a service tier, which is the part of the app that knows about units of work. Controllers call into services, which handle transactions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文