संभव डुप्लिकेट:
when to close Connection, Statement, PreparedStatement and ResultSet in JDBCअच्छा प्रथाओं: JDBC कनेक्शन
मैं एक JDBC कनेक्शन के लिए एक सरल आवरण लिखा है और यह काम करता है, लेकिन मैं सर्वोत्तम प्रथाओं के साथ यह सुधार करना चाहते हैं यथासंभव। इसमें मूल रूप से open()
, close()
, isOpened()
, select()
, insert()
, update()
, delete()
और batch()
जैसी विधियां हैं। सादगी के लिए मैं केवल पहले 4 तरीकों को पोस्ट करूंगा।
public class Query{
private Connection con;
private PreparedStatement ps;
private ResultSet rs;
//Database.open() returns a Connection ready to use
public void open (Database database) throws DatabaseException, SQLException{
if (!isOpened()){
con = database.open();
}
}
public void close() throws SQLException{
if (isOpened()){
if (ps != null) ps.close();
con.close();
con = null;
}
}
public boolean isOpened(){
return con != null;
}
//The query string is the query without the word "select" and can use placeholders (?)
//The args param it's just an array owith the values of this placeholders
public ResultSet select (String query, Object[] args) throws SQLException{
if (ps != null) ps.close();
if (isOpened()){
ps = con.prepareStatement ("select " + query);
if (args != null){
for (int i=0; i<args.length; i++){
ps.setObject (i+1, args[i]);
}
}
rs = ps.executeQuery();
}
return rs;
}
}
नोट्स:
- समान क्वेरी वस्तु उदाहरण खोलने और, यह बंद करने के लिए और फिर से खोलने के बाद, पुन: उपयोग किया जा सकता है।
- मैं प्रत्येक क्वेरी के लिए कनेक्शन बंद करने नहीं कर रहा हूँ, मैं सिर्फ तैयार बयान बंद करने कर रहा हूँ (यह सही है या मैं तैयार बयान खोला क्योंकि कनेक्शन वस्तु यह बंद हो जाएगा छोड़ सकते हैं?)
- जब मैं
Connection
बंद करें, सभीPreparedStatement
एस और उनकेResultSet
एस भी बंद हैं, है ना?
उपयोग:
Database database;
//Database initialization
Query query = new Query();
query.open (database);
ResultSet rs = query.select ("* from user where name=?", new String[]{ "MyName" });
doSomethingWithResult1 (rs);
//Connection is not closed here
ResultSet rs = query.select ("coordx from point where coordy=? and coordz=?", new Float[]{ 0.1, 0.2 });
doSomethingWithResult2 (rs);
query.close();
query.open (database);
ResultSet rs = query.select ("* from user where name=?", new String[]{ "MyName" });
doSomethingWithResult1 (rs);
//Connection is not closed here
ResultSet rs = query.select ("coordx from point where coordy=? and coordz=?", new Float[]{ 0.1, 0.2 });
doSomethingWithResult2 (rs);
query.close();
आप क्या सोचते हैं? क्या मुझे प्रत्येक क्वेरी के बाद कनेक्शन बंद करना और खोलना चाहिए? क्या मैं उसी कनेक्शन पर प्रत्येक क्वेरी के बाद प्रीपेयरस्टेटमेंट खोला जा सकता हूं? यह एक अच्छा डिजाइन है?
प्रत्येक क्वेरी के लिए कनेक्शन बंद करना और पुनः खोलना एक बुरा विचार है। एक कनेक्शन बनाना वास्तव में महंगा है। – bdares
तो क्या मैं तैयार किए गए स्टेटमेंट को खोला जा सकता हूं या क्या मुझे इसे प्रत्येक क्वेरी के लिए बंद करना चाहिए? –
जब तक आप वास्तव में डीबी का उपयोग नहीं कर लेते हैं तब तक आप सबकुछ खुला छोड़ सकते हैं। एक कनेक्शन बंद करने से यह सभी चीजों (बयान, परिणाम) को ट्रैक और बंद कर दिया जाएगा। – bdares