Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6466186

Connection is closed when DataSet is created using datasource

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P3 P3
    • None
    • 6
    • core-libs
    • None
    • generic
    • generic

      ========================================================================================
      JDK : java version "1.6.0-rc"
      Java(TM) SE Runtime Environment (build 1.6.0-rc-b98)
      Java HotSpot(TM) Client VM (build 1.6.0-rc-b98, mixed mode)
      DB : JavaDB
      Driver : JavaDB
      Platform[s] : All
      ========================================================================================

      When DataSet object is created using DataSource, a temporary connection is created and then closed. There is no check for DataSet being updatable/connected or not. This caused SQLRuntimeException to be thrown, when it should not.

      OUTPUT:
      -------
      Inserts Holger, Wu & John
      ------- 1. query & loop
      Found: Danske, Holger
      Found: Doe, John
      Found: Ming, Wu
      Exception in thread "main" java.sql.SQLRuntimeException: Cannot update ResultSet column for dataset field type
             at com.sun.sql.DataSetImpl.insert(DataSetImpl.java:495)
             at EODdsClose.main(EODdsClose.java:81)
      Caused by: java.sql.SQLTransientConnectionException: No current connection.
             at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
             at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
             at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
             at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
             at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
             at org.apache.derby.impl.jdbc.Util.noCurrentConnection(Unknown Source)
             at org.apache.derby.impl.jdbc.EmbedResultSet.checkExecIfClosed(Unknown Source)
             at org.apache.derby.impl.jdbc.EmbedResultSet.moveToInsertRow(Unknown Source)
             at com.sun.sql.DataSetImpl.insert(DataSetImpl.java:484)
             ... 1 more
      Caused by: java.sql.SQLException: No current connection.
             ... 10 more

      SOURCE:
      -------
      import java.sql.*;
      import javax.sql.*;

      public class EODdsClose
      {
         static String DB = "/export/home//tmp/DB/eod";

         static public class Person {
             public Person() {
                 // Needed for DataSet
                 firstName = null;
                 lastName = null;
             }
             public Person(String f, String l) {
                 firstName = f;
                 lastName = l;
             }
             public String firstName;
             @ResultColumn(uniqueIdentifier=true)
                 public String lastName;
             public String toString()
             {
                 if (firstName == null) {
                     return lastName;
                 } else {
                     return lastName + ", " + firstName;
                 }
             }
             public boolean equals(Person other)
             {
                 if (firstName == null) {
                     return lastName.equals(other.lastName);
                 } else {
                     return lastName.equals(other.lastName) &&
                         firstName.equals(other.firstName);
                 }
             }
         }

         public static DataSource getDS()
             throws SQLException
         {
             org.apache.derby.jdbc.EmbeddedDataSource40 ds =
                 new org.apache.derby.jdbc.EmbeddedDataSource40();

             // DatabaseName can include Derby URL Attributes
             ds.setDatabaseName(DB);
             ds.setCreateDatabase("create");

             return ds;
         }

         interface PersonQ extends BaseQuery {
             @Select(sql="select firstName, lastName from persons", readOnly=false)
                 DataSet<Person> getPersons();
             @java.sql.Update(sql="delete from persons")
                 int deleteAll();
             @java.sql.Update(sql="insert into persons values (?1,?2)")
                 int insertPerson(String a, String b);
             @java.sql.Update(sql="create table persons (firstName varchar(256), lastName varchar(256), primary key(lastName,firstName))")
                 int createTable();
         }

         static public void main(String []args)
             throws Exception
         {
             DataSource ds = getDS();
             PersonQ pqo = ds.createQueryObject(PersonQ.class);

             pqo.createTable();
             System.out.println("Inserts Holger, Wu & John");
             pqo.insertPerson("Holger","Danske");
             pqo.insertPerson("Wu","Ming");
             pqo.insertPerson("John", "Doe");

             System.out.println("------- 1. query & loop");
             DataSet<Person> dsp = pqo.getPersons();
             for (Person p: dsp) {
                 System.out.println("Found: " + p);
             }
             dsp.insert(new Person("Ola","Nordmann"));

             dsp.close();

             System.out.println("------- 2. query & loop");
             dsp = pqo.getPersons();
             for (Person p: dsp) {
                 System.out.println("Found: " + p);
             }
             dsp.close();
             pqo.close();
        }
      }

            ssharmasunw Sushmita Sharma (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: