Results 1 to 9 of 9
Thread: connection dillemna
- 12-27-2011, 02:44 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
connection dillemna
hi, i am developing a sample application using an embedded derby database, i have a code fragment that edits records on the database however i find that when i close my connection after editing by clicking a hide buton which hides the edit categories and closes the conection, i get a nontransientexception:no current connection error however when i leave the connection open, i dont get this error, i cnow its goo practice to close connections after use could somebody please direct me on this
- 12-27-2011, 04:45 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: connection dillemna
Post an SSCCE that demonstrates the problem.
- 12-28-2011, 05:31 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: connection dillemna
i dont quite get you, full meaning of sccee please?
- 12-28-2011, 06:20 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
Re: connection dillemna
Google...and the first hit is? ... Short, Self Contained, Correct Example
- 12-29-2011, 06:22 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: connection dillemna
Okay here is the scenario i have an edit button on my jFrame and when pressed it reveals some radio buttons with the editing categories and i have a jTextField where the user can input what he/she wants to edit a current record with and an update j button commits the changes to a derby database:
here is the code for the update button:
if(jTextField10.getText().isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Parameter Cant Be Empty");
return;
}
if(jRadioButton2.isSelected()) {
String sql = "update " + jLabel10.getText().trim() + " set Last_Name = " + "'" + jTextField10.getText().trim() + "'" + " where Admission = " + "'" + jTextField1.getText().trim() + "'";
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDrive r");
conn = DriverManager.getConnection(url);
pst = conn.prepareStatement(sql);
pst.executeUpdate();
UpdateJtable();
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane, e);
}finally{
jTextField10.setText("");
jRadioButton2.setSelected(false);
}
}
if(jRadioButton3.isSelected()) {
String sql = "update " + jLabel10.getText().trim() + " set First_Name = " + "'" + jTextField10.getText().trim() + "'" + " where Admission = " + "'" + jTextField1.getText().trim() + "'";
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDrive r");
DriverManager.getConnection(url);
pst = conn.prepareStatement(sql);
pst.executeUpdate();
//conn.commit();
UpdateJtable2();
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane, e);
}finally{
jTextField10.setText("");
jRadioButton3.setSelected(false);
}
}
try{
if(jRadioButton4.isSelected()) {
String sql = "update " + jLabel10.getText().trim() + " set Middle_Name = " + "'" + jTextField10.getText().trim() + "'" + " where Admission = " + "'" + jTextField1.getText().trim() + "'";
try{
//Class.forName("org.apache.derby.jdbc.EmbeddedDrive r");
DriverManager.getConnection(url);
pst = conn.prepareStatement(sql);
pst.executeUpdate();
//conn.commit();
UpdateJtable2();
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane, e);
}finally{
jTextField10.setText("");
jRadioButton4.setSelected(false);
}
}
if(jRadioButton5.isSelected()) {
String sql = "update " + jLabel10.getText().trim() + " set Sex = " + "'" + jTextField10.getText().trim() + "'" + " where Admission = " + "'" + jTextField1.getText().trim() + "'";
try{
/Class.forName("org.apache.derby.jdbc.EmbeddedDrive r");
DriverManager.getConnection(url);
pst = conn.prepareStatement(sql);
pst.executeUpdate();
//conn.commit();
UpdateJtable2();
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane, e);
}finally{
jTextField10.setText("");
jRadioButton5.setSelected(false);
}
}
if(jRadioButton7.isSelected()) {
String sql = "update " + jLabel10.getText().trim() + " set Paid = " + Float.parseFloat(jTextField10.getText().trim()) + " where Admission = " + "'" + jTextField1.getText().trim() + "'";
String sql2 = "update " + jLabel10.getText().trim() + " set Balance = " + (10000 - Float.parseFloat(jTextField10.getText().trim())) + " where Admission = " + "'" + jTextField1.getText().trim() + "'";
try{
Class.forName("org.apache.derby.jdbc.EmbeddedDrive r");
DriverManager.getConnection(url);
pst = conn.prepareStatement(sql);
pst2 = conn.prepareStatement(sql2);
pst.executeUpdate();
pst2.executeUpdate();
UpdateJtable2();
}catch(Exception e){
JOptionPane.showMessageDialog(rootPane, e);
}finally{
jTextField10.setText("");
jRadioButton7.setSelected(false);
}
}
}catch(Exception x){
JOptionPane.showMessageDialog(rootPane, x);
} finally{
callclose();
}
the callclose() method closes all the resources after use e.g connections,resultsets and statements the code fragment works however sometimes a java.sql.nontransientconnectionexception: no current connection is thrown, this also hapens when i deviate from editing and perform other sql functions likeb inserting and deleting then try to go back to editing records
here is the callclose() method
public void callclose(){
if (conn != null){
try{
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}if(pst !=null){
try{
pst.close();
}catch(Exception e){
System.out.println(e.getMessage());
}if(rs !=null){
try{
rs.close();
}catch(Exception e){
System.out.println(e.getMessage());
}if(pst2 !=null){
try{
pst2.close();
}catch(Exception e){
System.out.println(e);
}
}
}
}
}
}
however i have noticed a pattern that if i deviate from editing and perform other database functions like inserting which also has the callclose() method in the finally block andthen i again attempt to edit a record i get a java.sql.SQLNonTransientConnectionException: No current connection exception, but when i comment out this block in the finally method:
if (conn != null){
try{
conn.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
i dont get the exception and the program runs as expected
below is the stack trace
java.sql.SQLNonTransientConnectionException: No current connection.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.g etSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLExcepti on(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLExcepti on(Unknown Source)
at org.apache.derby.impl.jdbc.Util.noCurrentConnectio n(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.checkIf Closed(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.setupCo ntextStack(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepare Statement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepare Statement(Unknown Source)
at userpack.userframe.jButton8ActionPerformed(userfra me.java:1648)
at userpack.userframe.access$3000(userframe.java:48)
at userpack.userframe$30.actionPerformed(userframe.ja va:533)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6038)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3260)
at java.awt.Component.processEvent(Component.java:580 3)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429 )
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)
Caused by: java.sql.SQLException: No current connection.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.get SQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.w rapArgsForTransportAcrossDRDA(Unknown Source)
... 36 more
BUILD SUCCESSFUL (total time: 49 seconds)
I have even tried System.gc(): to clean up but nothing
i hope this conforms to the rules of SSCCE :-)
- 01-03-2012, 09:03 AM #6
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Re: connection dillemna
hello? ¿es ud hay?
- 01-03-2012, 01:13 PM #7
Re: connection dillemna
A SSCCE is a source file that can be copied from the forum into your editor, saved, compiled and executed.i hope this conforms to the rules of SSCCE :-)
Can that be done with what you have posted?
NO, First there isn't a class and a main()
- 01-03-2012, 02:16 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: connection dillemna
You're using an embedded database.
There's no point closing the connection, as there should only be the one anyway.
Just make sure that only one thread (if you end up with multi-threaded code) can access it at any one time.
You might also want to keep your PreparedStatements open as well. Saves having to re-prepare them.
Statements should be closed, as should result sets.
When posting code, use code tags.
- 01-08-2012, 10:00 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
tcp connection
By tsunamy_boy in forum Java AppletsReplies: 19Last Post: 08-06-2010, 08:42 PM -
Java.net.socket connection :connection closed
By veeru541 in forum Advanced JavaReplies: 2Last Post: 06-27-2010, 02:14 AM -
FTP connection
By java2010 in forum New To JavaReplies: 3Last Post: 04-19-2010, 09:46 AM -
we implement connection pooling ourselves, but why it always out of connection ?
By zengqingyi12 in forum JDBCReplies: 7Last Post: 10-20-2009, 10:34 AM -
TCP Connection
By Blacknight in forum New To JavaReplies: 3Last Post: 06-04-2009, 01:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks