使用连接池(本地或实例)的网站的连接对象范围
对于启用了连接轮询的 Web 应用程序,使用本地范围的连接对象还是实例范围的连接对象更好?我知道两者之间可能没有很大的性能改进(因为池化),但是你会说一个比另一个遵循更好的模式吗?谢谢;)
public class MyServlet extends HttpServlet {
DataSource ds;
public void init() throws ServletException {
ds = (DataSource) getServletContext().getAttribute("DBCPool");
}
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
SomeWork("SELECT * FROM A");
SomeWork("SELECT * FROM B");
}
void SomeWork(String sql) {
Connection conn = null;
try {
conn = ds.getConnection();
// execute some sql
.....
} finally {
if(conn != null) {
conn.close(); // return to pool
}
}
}
}
或者
public class MyServlet extends HttpServlet {
DataSource ds;
Connection conn;*
public void init() throws ServletException {
ds = (DataSource) getServletContext().getAttribute("DBCPool");
}
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
try {
conn = ds.getConnection();
SomeWork("SELECT * FROM A");
SomeWork("SELECT * FROM B");
} finally {
if(conn != null) {
conn.close(); // return to pool
}
}
}
void SomeWork(String sql) {
// execute some sql
.....
}
}
For a web application with connection polling enabled, is it better to work with a locally scoped connection object or instance scoped connection object. I know there is probably not a big performance improvement between the two (because of the pooling) but would you say that one follows a better pattern than the other. Thanks ;)
public class MyServlet extends HttpServlet {
DataSource ds;
public void init() throws ServletException {
ds = (DataSource) getServletContext().getAttribute("DBCPool");
}
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
SomeWork("SELECT * FROM A");
SomeWork("SELECT * FROM B");
}
void SomeWork(String sql) {
Connection conn = null;
try {
conn = ds.getConnection();
// execute some sql
.....
} finally {
if(conn != null) {
conn.close(); // return to pool
}
}
}
}
Or
public class MyServlet extends HttpServlet {
DataSource ds;
Connection conn;*
public void init() throws ServletException {
ds = (DataSource) getServletContext().getAttribute("DBCPool");
}
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
try {
conn = ds.getConnection();
SomeWork("SELECT * FROM A");
SomeWork("SELECT * FROM B");
} finally {
if(conn != null) {
conn.close(); // return to pool
}
}
}
void SomeWork(String sql) {
// execute some sql
.....
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该采用这种方法:
您的第一种方法不是一个好主意,因为如果工作单元失败可能会使数据库处于不一致的状态。
也许您将完成第一个工作单元,然后无法获取第二个池连接(其他一些线程首先进入)。也许在重负载下,多个请求都会发生这种情况,并且什么都无法正确完成。
You should take this approach:
Your first approach is not a good idea because if one of the units of work fails you may leave the database in an inconsistant state.
Perhaps you will complete the first unit of work and then be unable to take the second pool connection (some other thread got in first). Maybe under heavy load, this will happen to multiple requests and nothing will get done properly.