java


Is it necessary to close jdbc connection in localhost


I use static methods in a java project that i build, to make queries to a localhost mysql db.
Something like that:
public static void sqlQuery() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(localhost,root,password);
stmt = conn.prepareStatement(Some SQL);
rs = stmt.executeQuery();
} catch(Exception e) {
// Error Handling
} finally {
try { if (rs != null) rs.close(); } catch (Exception e) {};
try { if (stmt != null) stmt.close(); } catch (Exception e) {};
try { if (conn != null) conn.close(); } catch (Exception e) {};
}
}
My Question is if i can keep the conn variable without close it and reuse it again for another query.
No other app use this db and i can do that i want with only 1 connection..
P.S. Sorry for my bad english..
Connections are not thread-safe, so using only one is fraught with peril.You should use a connection pool, with a new logical connection for each logical activity.
You can reuse a connection in the same context. Once you have changed your context, you should close the former connection and reopen another connection.
You should always close resources like JDBC connection. If you don't close them, it creates a resource leak and it can slow down your program.
It is always advisable to close the connection, since its not a thread safe. It is also not advisable to keep the connection object as the singleton.

Related Links

ComboBoxViewerCellEditor and AutoCompleteField doesn't work
Add icon to a (link) element / element in element
Suppress Keyword Expansion in Shell Script
Mongo match in aggregate not working for date using java
Java method arguments of two types
Neo4j queries slows down but after restart are fast again
Execute linux command in java and display output to html table
Java Annotations javaArgs and LiteralArguments are deprecated
NoSuchMethodError while parsing xsd to generate classes using JAXB
Hibernate using sql call for each row to fetch relationship - instead of aggregating to one IN clause
How do I make a local variable increase consistently in a recursive function having more than one calls?
Intellij IDEA : Spark code running results in java.lang.VerifyError
JavaParser comparison
How To use setOnItemClickListner multiple times in a ListView?
NullPointer exception in a Spring Rest Controller test - Java
Most suitable Java Collection for duplicate elements

Categories

HOME
ibm-bluemix
multithreading
pdf
osgi
clips
deployment
omnet++
kde
smarty
electron
relay
rubygems
callback
ojdbc
gitpitch
gnupg
vault
quicklook
postgres-xl
alignment
commonmark
circuit
saxon
transformation
google-cloud-speech
visual-composer
plunker
errorlevel
ehcache
sylius
sox
tapestry
google-search-api
trading
publish
typo3-6.2.x
elasticsearch-net
msys2
exuberant-ctags
siesta-swift
uiswipegesturerecognizer
mpmediaquery
neuroscience
libvpx
code-search-engine
xenforo
streamreader
http-live-streaming
node-gyp
estimote
node-sass
angular2-meteor
google-datalayer
scrollable
theming
paxos
rdfs
mplayer
darcs
tactic
diagnostics
feeds
crypt
boost-preprocessor
knpmenubundle
gcsfuse
query-performance
ableton-live
paypal-express
packagist
captivenetwork
jfugue
ios8-today-widget
separator
lemon
simple-framework
iis-arr
proj4js
streambase
operator-precedence
oxwall
camanjs
gadt
android-imagebutton
file-copying
flask-cors
jquery-layout
dealloc
ember-charts
centos5
spring-io
http-unit
browser-detection
hamiltonian-cycle
path-separator
unc
coderush
dbproviderfactories
work-stealing
qt-jambi
tomcat-valve
xtype
mysql-error-1005
perfect-hash
zend-tool
sef
photoshop-cs4
project-hosting
multiple-languages

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App