jdbc 单例的问题

java_8dc 2010-07-15 09:21:38
网上有很多人把jdbc连接数据库写成了单例模式,这种方式可行吗? 多个线程都拿的是同一个Connection, 去操作数据库,这应该不行吧?
还有一个问题,如果用户只对数据库进行查询操作,那可不可以把Connection写成单例模式?
用人会用单例模式去获取一个数据库边接吗? 如果有,那么用在什么场景?
感谢大家的讨论,谢谢!
...全文
271 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaohui5850 2010-07-16
  • 打赏
  • 举报
回复
Famous global IT company 上海
Position: Senior Java Software Engineer
Business Unit: Famous global IT company - BAS - China - ITSE

Responsibilities
 Lead the development of complex software product and mobile applications.
 Design layered application, including user interface, business functionality, and database access.
 Work with other engineers, managers, product managers, QA, and operation teams to develop innovative solutions that meet market needs with respect to functionality, performance, scalability, and reliability while meeting realistic implementation schedules and adhering to development goals and principles.
 Estimate engineering efforts, plan implementations, and rollout system changes.
 Share release management duties during feature rollouts and share on-call responsibilities.
 Developing, unit testing, maintenance and release of software products under Agile patterns.
 Experience in developing highly scalable applications a major plus.


Job Requirements
 BS/BA in CS or related field.
 5+ years experience in requirements analysis, design, coding and testing of scalable, distributed, fault-tolerant applications.
 Expertise required in object-oriented design methodology and enterprise application development in Java .
 Hands-on experience with J2EE/J2SE application.
 Hands-on experience in Agile projects.
 Good at OO methodology, and familiar with Java design patterns.
 Proven result-oriented person with a delivery focus in a fast pace, high quality environment.
 Self-motivated and work under pressure.
 Solid capabilities of self study for new technologies.
 Knowledge of software product development is a major plus
 Fluent English in both speaking and writing.

有意者加MSN:emmafhnj@hotmail.com
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 java_8dc 的回复:]
请问火龙果
在不用连接池的情况下:
1. 那请问一个页面只有查询数据库的操作,也应该为每个请求创造一个Connection吗
2. 如果要为每个请求创建一个Connection,那我的JDBC连接应该写在什么地方?并且在什么地方关闭?
3. 如何做到当浏览器开启时创建连接,浏览器关闭时也断开连接?
[/Quote]

我想你应该知道 JDBC 代码的套路是什么,在这里我只能回答你,按照套路把 Connection 弄成局部变量。
java_8dc 2010-07-16
  • 打赏
  • 举报
回复
请问火龙果
在不用连接池的情况下:
1. 那请问一个页面只有查询数据库的操作,也应该为每个请求创造一个Connection吗
2. 如果要为每个请求创建一个Connection,那我的JDBC连接应该写在什么地方?并且在什么地方关闭?
3. 如何做到当浏览器开启时创建连接,浏览器关闭时也断开连接?
  • 打赏
  • 举报
回复
JDBC 规范并没有规定 Connection 必须是线程安全的,因此,JDBC 厂商也不会将其做为线程安全的。
  • 打赏
  • 举报
回复
不可行!

总之,如果在多线程处理上没有很强的技术,不能将数据库连接的那三个对象写成成员变量,更不能使用单例。

具体怎么不可行,需要看具体的代码。
xk1126 2010-07-16
  • 打赏
  • 举报
回复
干吗呀!真是的!
一般都写个类封装啊!~~
bb5209027 2010-07-16
  • 打赏
  • 举报
回复
JDBC 6个步骤:
1. Register driver
3 ways: * Class.forName(driverName);
* Driver dr=new XXXXDriver();
* java -Djdbc.drivers=driverName[:driverName] className

2. Establish a connection to the Database
2 ways * Connection con=DriverManager.getConnection(databaseURL, userID, password);
* Connection con=dr.connect(urlString, propertiesInfo);

3. Create a statement
3 statements:
* Statement st=con.createStatement();
* PreparedStatement pst=con.prepareStatement(sqlString);
* CallableStatement

4. Executing SQL
* st.execute(sqlString); //pst.setXXXX(?,?); pst.execute();
* ResultSet rs=st.executeQuery(sqlString); // pst.setXXXX(?,?); ResultSet rs=pst.executeQuery();
* st.executeUpdate(sqlString); // pst.setXXXX(?,?); pst.executeUpdate();

5. Processing ResultSet
while(rs.next())
XXX x= rs.getXXX(?);// !!!!!! data type (java <-> Sql ), ? : columnName|columnIndex

6. close : rs, st, pst, con
按这个来,一般据ok了
victor_woo 2010-07-15
  • 打赏
  • 举报
回复
查询一个连接 ,多Statement
更新一个连接,多Statement


事务处理多个连接

适用小型应用
victor_woo 2010-07-15
  • 打赏
  • 举报
回复
如果用户只对数据库进行查询操作,那可不可以把Connection写成单例模式?

一个连接可以有多个Statement对象并发支持多用户同时查询
jumpheightway 2010-07-15
  • 打赏
  • 举报
回复
ThreadLocal吧

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧