-1

What is the best way to use connection in java application that having around 100 classes. 1) When user logging to the system, create a connection and pass to other classes and close when user presses exit button. or, 2) create a connection within every method and close when method ends.(Methods like we use for get data from db)

4

2 回答 2

2

标准的答案是使用连接池,它经常在连接过多和并发所需的连接数之间取得适当的平衡。看看http://docs.oracle.com/javase/7/docs/api/javax/sql/ConnectionPoolDataSource.html

于 2013-08-24T17:15:14.470 回答
0

一如既往,这取决于。您不能在 2013 年合法地打开数百万个数据库连接,这是一个性能问题。您要做的是缓存尽可能多的数据,以减少到数据库的往返次数。通常,您只希望在使用时打开连接,不需要打开幻像连接。

于 2013-08-24T17:13:36.417 回答