servlet中的并发问题 [英] Concurrency Issue in servlet

查看:144
本文介绍了servlet中的并发问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于并发的问题.想象一下,来自世界各地的10个不同用户的10个不同的doGet()请求在几毫秒内彼此调用到一个名为LoginServlet.java的servlet. Servlet的doGet()方法内部是对DbUtil.getConnection()的调用. DBUtil类是静态的,方法getConnection()也是静态的.发生什么事了?有哪些可能的竞争条件,并发问题等?连接之间或某些东西之间可能存在损坏?对于连接池,这种设计是否还有更好的选择?

Question regarding concurrency. Imagine 10 different doGet() requests from 10 different users around the world called within milliseconds of each other to a servlet called LoginServlet.java. Inside the servlet's doGet() method is a call to DbUtil.getConnection(). The DBUtil class is static and the method getConnection() is also static. What happens here? What are possible race conditions, concurrent issues, etc etc? Can there be corruption between connections or something? Is there a better alternative to this design for connection pooling?

只需要像LoginServlet.java这样做就可以了.

Just picture LoginServlet.java doing something like this.

@WebServlet("/LoginServlet.secure")  
public class LoginServletextends HttpServlet {  
    private static final long serialVersionUID = 1L;  

    public LoginServlet() {  
        super();  
    }  

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        Connection connection = DbUtil.getConnection();  
    }  

这是用于获取连接的静态DBUtil类.

This is the static DBUtil class to get connections.

package util;    

import java.sql.Connection;    
import java.sql.SQLException;    

import javax.naming.InitialContext;    
import javax.naming.NamingException;    
import javax.sql.DataSource;    

public class DbUtil {    

    private static DataSource dataSource;    

    static {    
        try {    
            dataSource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/MyDataSource");    
        } catch (NamingException e) {     
            throw new ExceptionInInitializerError("'jdbc/MyDataSource' not found in JNDI");    
        }    
    }    

    public static Connection getConnection() throws SQLException {    
        return dataSource.getConnection();    
    }    

}   

推荐答案

我实际上在这里实现了与您非常相似的东西,而这就是我实现它的方式:

I am actually implementing something fairly similar with you have here, and this was how I implemented it: See my answer here

尽管我的设计中有一些元素需要根据

Although there are a few elements in my design that need to be changed based on this answer from BalusC, such as moving my Logger instantiation inside of the doGet, since my Logger is not of a thread-safe design inherently. My DBEngine, however, is designed to support concurrency, and it makes heavy use of PostgresQL's natural row-level-locking.

可悲的是,我还没有完全完成这个项目,所以我不能确切说出要避免的陷阱,但是我可以在几个月内扩大这个答案.但这将使您对我在该主题上的研究工作有个很好的了解.

Sadly, I have not fully completed this project yet, so I cannot tell exactly which pitfalls to avoid, but I will be able to expand this answer in a few months. But this will give you a good idea of the sum of my research on this topic.

这篇关于servlet中的并发问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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