将jdbc操作放在actor中好吗? [英] Is it good to put jdbc operations in actors?

查看:152
本文介绍了将jdbc操作放在actor中好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个传统的Web应用程序,该应用程序通过JDBC执行数据库CRUD操作.而且我想知道将jdbc操作放入actor,从当前的请求处理线程中取出是否很好.我进行了一些搜索,但没有找到演示该示例的教程或示例应用程序.

那么什么是利弊?这种异步化是否会像nio一样提高应用服务器的容量(即处理并发请求)?

解决方案

是否将JDBC访问置于actor中是好",还是在很大程度上取决于应用程序的其余部分.

由于 Servlet API ,它是大多数Java(和Scala)Web框架的基础.虽然我们现在看到对异步servlet的支持,但该支持并未奏效所有框架.除非您从支持异步处理的框架开始,否则您的请求处理将是同步的.

对于JDBC, JDBC是同步的.实际上,考虑到修改世界上千篇一律的JDBC驱动程序实现所带来的负担,对此永远不会做任何事情.我们可以希望,但不要屏住呼吸.

并且JDBC实现本身不一定必须是线程安全的,因此在对同一连接执行某些其他操作之前调用JDBC连接上的操作将导致未定义的行为.还有未定义的行为!=好.

所以我的猜测是,您不会看到与NIO完全相同的容量改进.

编辑 :刚刚发现 adbcj ;异步数据库驱动程序API.这是一个为硕士论文编写的实验性项目,很早就具有实验性.这是一个值得的实验,我希望它能成功.看看吧!

但是,如果您要构建一个基于参与者的异步系统,我真的很喜欢拥有数据访问权限或存储库参与者的想法,就像您拥有未来,它们是可组合.这样可以更轻松地将数据访问与其他异步活动进行协调.

那么,好吗?可能的话,如果它适合您系统其余部分的体系结构.会提高容量吗?这将取决于您的整个系统,但这听起来像是一个非常值得的实验.

I am building a traditional webapp that do database CRUD operations through JDBC. And I am wondering if it is good to put jdbc operations into actors, out of current request processing thread. I did some search but found no tutorials or sample applications that demo this.

So What are the cons and pros? Will this asynchonization improve the capacity of the appserver(i.e. the concurrent request processed) like nio?

解决方案

Whether putting JDBC access in actors is 'good' or not greatly depends upon the rest of your application.

Most web applications today are synchronous, thanks to the Servlet API that underlies most Java (and Scala) web frameworks. While we're now seeing support for asynchronous servlets, that support hasn't worked its way up all frameworks. Unless you start with a framework that supports asynchronous processing, your request processing will be synchronous.

As for JDBC, JDBC is synchronous. Realistically there's never going to be anything done about that, given the burden that would place on modifying the gazillion JDBC driver implementations that are out in the world. We can hope, but don't hold your breath.

And the JDBC implementations themselves don't have to be thread safe, so invoking an operation on a JDBC connection prior to the completion of some other operation on that same connection will result in undefined behavior. And undefined behavior != good.

So my guess is that you won't see quite the same capacity improvements that you see with NIO.

Edit: Just discovered adbcj; an asynchronous database driver API. It's an experimental project written for a master's thesis, very early, experimental. It's a worthy experiment, and I hope it succeeds. Check it out!

But, if you are building an asynchronous, actor-based system, I really like the idea of having data access or repository actors, much in the same way your would have data acccess or repository objects in a layered OO architecture.

Actors guarantee that messages are processed one at a time, which is ideal for accessing a single JDBC connection. (One word of caution: most connection pools default to handing out connection-per-thread, which does not play well with actors. Instead you'll need to make sure that you are using a connection-per-actor. The same is true for transaction management.)

This allows you to treat the database like the asynchronous remote system we ought to have been treating it as all along. This also means that results from your data access/repository actors are futures, which are composable. This makes it easier to coordinate data access with other asynchronous activities.

So, is it good? Probably, if it fits within the architecture of the rest of your system. Will it improve capacity? That will depend on your overall system, but it sounds like a very worthy experiment.

这篇关于将jdbc操作放在actor中好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆