简体   繁体   English

Web 应用 DAL 中的 JDBC 数据库连接

[英]JDBC Database Connections in a Web App DAL

I am building a small website for fun/learning using a fairly standard Web/Service/Data Access layered design.我正在使用相当标准的 Web/服务/数据访问分层设计构建一个用于娱乐/学习的小型网站。

For the Data Access Layer, what is the best way to handle creating Connection objects to call my SQL stored procedures and why?对于数据访问层,处理创建连接对象以调用我的 SQL 存储过程的最佳方法是什么?为什么? Bearing in mind I am writing a lot of the code by hand (I know I could be using Hibernate etc to do a lot of this for me)...请记住,我正在手动编写大量代码(我知道我可以使用 Hibernate 等为我做很多事情)...

1) Should I create one static instance of the Connection and run all my querys through it or will this cause concurrency problems? 1) 我应该创建 Connection 的一个静态实例并通过它运行我的所有查询还是会导致并发问题?

2) Should I create a Connection instance per database call and accept the performance overhead? 2) 我应该为每个数据库调用创建一个 Connection 实例并接受性能开销吗? (I will look into connection pooling at a later date if this is the case) (如果是这种情况,我将在稍后研究连接池)

You should use one Connection per thread.您应该为每个线程使用一个Connection Don't share connections across threads.不要跨线程共享连接。

Consider using Apache DBCP .考虑使用Apache DBCP This is a free and standard way of configuring database connections and drawing them from a pool.这是配置数据库连接并从池中提取它们的一种免费且标准的方法。 It's the method used by high-performance web servers like Tomcat.这是 Tomcat 等高性能 Web 服务器使用的方法。

Furthermore, if you're using DBCP, since it's a pool (read: cached), there's little penalty to creating/closing connections frequently.此外,如果您使用 DBCP,由于它是一个(读取:缓存),因此频繁创建/关闭连接几乎没有损失。

The standard way is to set up a DataSource.标准方法是设置数据源。 All application servers are able to do so via their admin console.所有应用程序服务器都可以通过其管理控制台执行此操作。 The pool is then accessible by it's JNDI name (eg "jdbc/MyDB").然后可以通过其 JNDI 名称(例如“jdbc/MyDB”)访问该池。

The data source should, in fact, be a connection pool (and usually is).事实上,数据源应该是一个连接池(通常是)。 It caches connections, tests them before passing to the application and does a lot of other important functions.它缓存连接,在传递给应用程序之前对其进行测试,并执行许多其他重要功能。

In your code you:在您的代码中,您:

  1. resolve JNDI name and cast it into DataSource解析 JNDI 名称并将其转换为 DataSource
  2. get a connection from the data source从数据源获取连接
  3. do your work做你的工作
  4. close the connection (it goes back to the pool here)关闭连接(它回到这里的池中)

You can set up the pool yourself (using any of freely available pool implementation), but it really doesn't make any sense if you're using an application server.您可以自己设置池(使用任何免费可用的池实现),但如果您使用的是应用程序服务器,这确实没有任何意义。

PS Since it's a web application a good way to make sure you have closed your connection after the request is to use HttpFilter. PS由于它是一个Web应用程序,因此确保在请求后关闭连接的好方法是使用HttpFilter。 You can set up one in web.xml.您可以在 web.xml 中设置一个。 When the request comes, acquire the connection, put it into ThreadLocal.当请求到来时,获取连接,放入ThreadLocal。 During the request, get the connection from ThreadLocal, but never close it.在请求期间,从 ThreadLocal 获取连接,但永远不要关闭它。 After the request, in the filter, close the connection.请求后,在过滤器中,关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM