java - Web 应用程序中的线程(servlet、Spring、hibernate)

标签 java spring hibernate servlets

我了解了独立应用程序中线程的基本概念。但是,在以下方面感到困惑。

1).在 Java Web 应用程序中(基于 servlet 和 Spring)?

我希望每个请求都由不同的线程处理。它是否正确?还有其他可用的定义吗?

2). Spring MVC 中的 Hibernate 线程是什么? session 工厂是线程安全的。而 session 对象则不是。这里的线程是什么意思?

请帮助我理解这一点。

最佳答案

1)应用程序服务器有一个线程池,当有请求到来时,它会从池中分配一个线程。同一个线程调用调度程序servlet,后者调用 Controller ,后者调用服务等,最后创建 HttpResponse 并将其发送到客户端。

2) Hibernate 的常见模式(如果您不使用 Seam 对话)是 session-per-request :

2.4.2. Session-per-request pattern

This is the most common transaction pattern. The term request here relates to the concept of a system that reacts to a series of requests from a client/user. Web applications are a prime example of this type of system, though certainly not the only one. At the beginning of handling such a request, the application opens a Hibernate Session, starts a transaction, performs all data related work, ends the transaction and closes the Session. The crux of the pattern is the one-to-one relationship between the transaction and the Session.

Spring 将事务存储在线程局部变量中。因此该线程有一个 Hibernate session (仅限于该线程),并且它与一个事务(或事务堆栈,因为它们可以嵌套)关联。

关于java - Web 应用程序中的线程(servlet、Spring、hibernate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23913297/

相关文章:

java - 监听tomcat realm认证事件

java - 为什么我的程序没有打印出我想要的内容?

java - Gradle未在intelliJ idea 2016.2中下载依赖项

java - 替换 Controller 中的文本并将其放入属性文件中

java - 结合JBehave和SpringJUnit4ClassRunner实现事务回滚

java - 无法理解Calendar类的before函数

java - spring中如何通过xml文件将值传递给多个参数化的构造函数?

java - 初始 SessionFactory 创建失败.org.hibernate.HibernateException : Missing column

hibernate - Spring 数据JPA : save list of parents where children have been moved between parents in one transaction

java - Spring boot JPA Hibernate 返回嵌套的 Json 对象 : Reddit style comment system