Results 1 to 9 of 9
  1. #1
    towerz is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default 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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: connection dillemna

    Post an SSCCE that demonstrates the problem.

  3. #3
    towerz is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: connection dillemna

    i dont quite get you, full meaning of sccee please?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: connection dillemna

    Quote Originally Posted by towerz View Post
    i dont quite get you, full meaning of sccee please?
    Google...and the first hit is? ... Short, Self Contained, Correct Example

  5. #5
    towerz is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default 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 :-)

  6. #6
    towerz is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: connection dillemna

    hello? żes ud hay?

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,273
    Rep Power
    25

    Default Re: connection dillemna

    i hope this conforms to the rules of SSCCE :-)
    A SSCCE is a source file that can be copied from the forum into your editor, saved, compiled and executed.
    Can that be done with what you have posted?
    NO, First there isn't a class and a main()

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default 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.

  9. #9
    towerz is offline Member
    Join Date
    Dec 2011
    Posts
    10
    Rep Power
    0

    Default Re: connection dillemna

    thanks allot tolls i appricaite it, however like i wrote i had noticed recently that maybe i should be leaving that connection open, al be sure to use code tags next time am only a newbie to this forums and this is my first post... muchas gracias

Similar Threads

  1. tcp connection
    By tsunamy_boy in forum Java Applets
    Replies: 19
    Last Post: 08-06-2010, 08:42 PM
  2. Java.net.socket connection :connection closed
    By veeru541 in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2010, 02:14 AM
  3. FTP connection
    By java2010 in forum New To Java
    Replies: 3
    Last Post: 04-19-2010, 09:46 AM
  4. Replies: 7
    Last Post: 10-20-2009, 10:34 AM
  5. TCP Connection
    By Blacknight in forum New To Java
    Replies: 3
    Last Post: 06-04-2009, 01:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •