Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to topicflat modethreaded modego to previous topicgo to next topicgo to back
作者 一个web应用程序应该以何种方式建立数据库连接?
jimmyofth





发贴: 18
于 2008-01-12 17:09 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
完成一个web应用程序,是一个session建立一个数据库连接,还是在application,或者每次执行sql语句都打开一个新连接?

我现在是这样做的:

1 新建一个类,类中有一个方法getConn。
  public Connection getConn() throws Exception {
    Class.forName(driver).newInstance();
    Connection conn = DriverManager.getConnection(url, user, password);
    return conn;
  }
url, user, password作为私有属性都指定了。

2 其他的 .class, .jsp需要与数据库交互时,加入如下语句
      OperateDB odb = new OperateDB();
      Connection conn = odb.getConn();
然后用这个conn来执行SQL,使用完后关闭。

本人也觉得这个是一个不理想的解决方法,效率肯定很低,由于我手头开发的是一个很少用户并发的系统,所以觉得可能不会影响太大。但今天在调试一个程序时,会经常出现一下错误:
org.apache.jasper.JasperException: [Microsoft][SQLServer 2000 Driver for JDBC]Software caused connection abort: socket write error
  org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

当然,并不是每次都会出错,我在出错的jsp里多次进行建立连接,断开连接的操作,估计有4-5次吧。所以,我怀疑是数据库连接使用的方法不对。

我对于java开发不是很熟悉,但我以前做过asp开发,asp的解决办法是:我在global.asa中建立好数据库连接,整个应用程序都是用这个连接。

我想请教各位大牛:
1 我查了一些资料,java里面也有jsa的使用,我是否可以照搬asp的做法,把数据库连接放在application中?

2 如果在session中建立一个连接,每个用户使用一个也可以

3 能否给我一个在开发系统中如何处理这类问题的例子?

谢谢!!



作者 Re:一个web应用程序应该以何种方式建立数据库连接? [Re:jimmyofth]
jimmyofth





发贴: 18
于 2008-01-12 17:22 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我使用的是tomcat+MS sqlserver


作者 Re:一个web应用程序应该以何种方式建立数据库连接? [Re:jimmyofth]
junyuo





发贴: 95
于 2008-01-13 23:06 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
hope this link can help you: http://www.ibm.com/developerworks/tw/library/j-webdata/


OCAJ, SCJP, SCWCD, SCBCD, OCEJPA, OCUP Fundamental, OCA, BEA 8.1 Certified Administrator, IBM Certified SOA Solution Designer
作者 Re:一个web应用程序应该以何种方式建立数据库连接? [Re:jimmyofth]
jimmyofth





发贴: 18
于 2008-01-14 11:03 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
The linked article that you gave me didn't descripe how to use connection pool. My probelem is how to avoid to create too many database connections in an web application.


作者 Re:一个web应用程序应该以何种方式建立数据库连接? [Re:jimmyofth]
JiafanZhou



版主


发贴: 736
于 2008-01-16 22:15 user profilesend a private message to usersend email to JiafanZhoureply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
It is basically a database connection pool issue. You have two options here.

1. Develop your own connection pool by googling yourself.
There are dozens of solutions available on the Internet, I am not going to give you the sugars here.

2. Downloading and use an open source connection pool project, for example. apache connection pool. This also involves some time to study and understand the framework.

Hope this helps.



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.
作者 Re:一个web应用程序应该以何种方式建立数据库连接? [Re:jimmyofth]
jimmyofth





发贴: 18
于 2008-01-19 10:37 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我把连接放在session里,这个算连接池吗?
但这样,我每次都得把request对象传给javaBean



作者 Re:一个web应用程序应该以何种方式建立数据库连接? [Re:jimmyofth]
chengbd



版主


发贴: 687
于 2008-01-20 17:02 user profilesend a private message to usersend email to chengbdreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
应该是用连接池了。最好是找开源的实现学习一下。

不过,最实用的办法,就是利用hibernate,再配置一些开源的连接池了。

这结东西,很少用自己来关心了,研究的价值不大。



知难而进

reply to topicflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923