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

JUnit tests for POJOs
implementing a “window manager” for HTML in a JSP custom tag
Making a very large Java array
Using excel as UI without VB
Scala and interfaces
JDBC Thin Driver: Invalid Packet Lenght [sic]
Standard Entitlements Model
Why is there no GIL in the Java Virtual Machine? Why does Python need one so bad?
different ways to define a method in an interface
How to tell which SHIFT key was pressed?
Best practice for parameter naming in Java constructors and simple setters
how do I change persistence.xml at run time
Is there any speed/efficiency benefit to setting a class' instance variables in a constructor rather than in their declaration if they'll have a “default” value?
Copying bulk documents (pdf files) from server to client machine with one click?
Hibernate one to zero or one mapping
Deriving Class from Generic T

Categories

HOME
ms-access
winforms
client
openlayers
date
nullpointerexception
json.net
homebrew
grep
webstorm
mean-stack
cmd
spring-jdbc
pivotal-cloud-foundry
in-app-purchase
amazon-cloudformation
gitpitch
awesome-wm
dacpac
reveal.js
spring-xd
rascal
modelica
circular-dependency
leiningen
zebra-printers
ghc
systemc
invantive-sql
visual-composer
scichart
cx-freeze
mps
chromium-embedded
icloud-api
angular2-aot
javacv
tdd
chromebook
instant-messaging
fabric8
http-status-code-503
language-concepts
ios5
xcode-extension
gesture
webdriver-manager
dynamic-reports
reportingservices-2005
angular-resource
hibernate-tools
qsslsocket
pebble-watch
elgg
slickedit
worker-thread
directory-structure
magma
lift-json
pg-dump
libusb-win32
windows-iot-core-10
pillow
spring-cache
sonarlint-vs
eventkit
abcpdf9
setuptools
android-fonts
grails-tomcat-plugin
disque
qtableview
collapse
quicklisp
security-testing
angular-leaflet-directive
cakephp-3.1
ios9.1
sysinternals
operation
sniffer
dereference
bridge.net
e10s
device-orientation
umbraco6
geonetwork
winddk
codeigniter-routing
socketexception
client-side-templating
websocket4net
issuu
neolane
dml
navigationservice
p4java
transcoding
listings
cos
padarn
django-nonrel
sqlperformance
jelly
delphi-6
free-variable
coderush
errai
assembly-loading
custom-backend
zpt
deobfuscation
winbugs14
mongomapper
h.323
user-friendly
exchange-server-2003
adrotator
mtj
premature-optimization
noscript
zune
.net-1.0

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