【问题标题】:Correct design and concurrency issues of servlet/jdbcservlet/jdbc的正确设计和并发问题
【发布时间】:2014-01-26 02:21:37
【问题描述】:

我有几个关于正确设计和并发性的问题想问。例如,我创建了一个简单的应用程序,它通过 servlet 获取参数并添加到数据库。所以过程是这样的。 1) 将名字/姓氏发送到 servlet 2) Servlet 调用 PersonDao.createPerson(firstname, lastname)。

涉及的类... PersonDao(接口) PersonDaoImpl(具体类) AbstractDao(抽象类) 个人控制器(Servlet)

如果这是一个设计正确的连接池代码,我想知道您的所有意见。数据源的静态创建是否正确?您会更改 AbstractDao 类中可能引起并发问题的任何内容吗?

public interface PersonDao {
    public void createPerson(String firstname, String lastname);
}

_

public class PersonDaoImpl extends AbstractDao implements PersonDao {

@Override
public void createPerson(String firstname, String lastname) {

    String query = " insert into persons values (?,?) ";

    Connection connection = null;
    PreparedStatement ps = null;
    try {
        connection = getConnection();
        ps = connection.prepareStatement(query);
        ps.setString(1, firstname);
        ps.setString(2, lastname);
        ps.executeUpdate();

    } catch (SQLException e) {
        System.out.println(e.toString());
    } finally {
        close(connection, ps, null);
    }

}
}

_

public abstract class AbstractDao {

protected 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");
    }
}

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

protected void close(Connection connection) {
    close(connection, null, null);
}

protected void close(Connection connection, Statement ps) {
    close(connection, ps, null);
}

protected void close(Connection connection, Statement ps, ResultSet rs) {

    try {
        if (rs != null)
            rs.close();

        if (ps != null)
            ps.close();

        if (connection != null)
            connection.close();

    } catch (SQLException e) {
        e.printStackTrace();
    }

}

}

-

@WebServlet("/PersonController")
public class PersonController extends HttpServlet {
private static final long serialVersionUID = 1L;

public PersonController() {
    super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");

    PersonDao personDao = new PersonDaoImpl();
    personDao.createPerson(firstname, lastname);

}

}

我的另一个问题是这里是否存在并发问题,特别是在 servlet 中。想象一下 1000 个请求同时命中 servlet。让我担心的是 PersonDaoImpl。
1000 个不同的线程,每个线程都有自己的堆栈。所以有 1000 个不同的 PersonDaoImpl 实例。如果我们使用 AbstractDao,它会在数据源上调用 getConnection。

所以问题是...... getConnection() 是否会造成并发问题? 1000个不同的请求能否对上述代码中的数据源对象造成威胁?
如果有一个私有的 PersonDao personDao = new PersonDaoImpl() 作为 servlet 中的实例怎么办。现在会发生什么?

我真正感到困惑的是当 PersonDaoImpl 被实例化时 doGet 内部发生了什么。有人可以给我一个演练吗。我的问题的要点是我那里的代码是否是线程安全的。

【问题讨论】:

  • 您使用的是启用 EJB 的容器吗?
  • 不,基本 servlet/jdbc

标签: multithreading servlets jdbc


【解决方案1】:

具有讽刺意味的是,我刚刚回答了 10 月份的一个问题。

See my answer here.

【讨论】:

    猜你喜欢
    • 2012-12-24
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多