Problem with Database while using JNDI Lookup.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Problem with Database while using JNDI Lookup.

    I thought that when the connection gets closed then all resources get closed .
    I also included the ResultSet and Statement ;) . But if the Statement closed then only ResultSet closed. But now i got the point ..after closing Connection i am still able to access to iterate the ResultSet . Actually it is tested only with Postgres. Is it sun specified : ?

    Now i tested another code ...
    Code:
    import org.apache.tomcat.dbcp.dbcp.cpdsadapter.DriverAdapterCPDS;
    import org.apache.tomcat.dbcp.dbcp.datasources.SharedPoolDataSource;
    
            DriverAdapterCPDS l_adapter = new DriverAdapterCPDS();
            l_adapter.setDriver("org.postgresql.Driver");
            l_adapter.setUrl("jdbc:postgresql://10.29.32.68:5432/Admin");
            l_adapter.setUser("erp");
            l_adapter.setPassword("iiterp");
            
            SharedPoolDataSource l_ds = new SharedPoolDataSource();
            l_ds.setConnectionPoolDataSource(l_adapter);
            Connection l_con = l_ds.getConnection();
            
            Statement l_stmt = l_con.createStatement();
            ResultSet l_rs = l_stmt.executeQuery("select * from dpcode");
            l_con.close();
            while(l_rs.next()){System.out.println("Yahoo!!!");}
    The Resultset is still getting iterated ...

    But when I get the DataSource using the JNDI look up then my another code is not working ..Saying that the ResultSet is closed.
    Could you help me to figure out ...
    Code:
                Context l_ctx = new InitialContext();
                DataSource l_ds =                     (DataSource)l_ctx.lookup("java:/comp/env/jdbc/admindatasource");
                Connection l_con = l_ds.getConnection();
            
                Statement l_stmt = l_con.createStatement();
                ResultSet l_rs = l_stmt.executeQuery("select * from dpcode");
                l_con.close();
                while(l_rs.next()){System.out.println("Yahoo!!!");}
    Please help me to figure out .. ;)
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    A Connection is the owner of a Statement. A Statement is the owner of a ResultSet. When you close an object earlier in the chain the other objects, later in the chain, are also closed. A PooledConnectio n isn't closed when you get rid of it, i.e. it is simply returned to its pool. Read the API documentation for the close() method of a Connection: "If the close method is called and there is an active transaction, the results are implementation-defined. "

    kind regards,

    Jos

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Then why does it happen to me ???
      U mean that whats are happening that should not be ?
      I could not get you ..yeah i know .when a connection(obta ined from pool) closed then it simply goes to Pool and be reused later.
      But these things are happening to me ?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        What part of "implementa tion-defined" was so difficult to understand? You're closing an object earlier in the chain: a Connection; nothing can be said about a Statement or ResultSet. The Connection could just be dumped in a pool again or it may be really closed.

        kind regards,

        Jos

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Yeah all these things are implementation dependent ... But still there should be a standard logic to implement ....

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by dmjpro
            Yeah all these things are implementation dependent ... But still there should be a standard logic to implement ....
            Yes there is. Close the rs first, then the statement and then the connection.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Then why does it behave like a hell ???
              Yeah i know ... i should close those resources sequentially ... But the code already developed .. it requires a lot of hurdle ...
              That's why i was asking .. if make a raw connection and even also using a DataSource it working fine .. but if i get the DataBase using JNDI look up then it's not working .. i was looking for this answer ?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dmjpro
                Then why does it behave like a hell ???
                Because the code assumed a particular implementation defined behaviour; that's an error; e.g. in C and C++ you have to stay far away from "undefined behaviour" and "implementa tion defined behaviour". Same here: there's an error in the code; fix it and don't ask why anymore.

                kind regards,

                Jos

                Comment

                • dmjpro
                  Top Contributor
                  • Jan 2007
                  • 2476

                  #9
                  Don't get me wrong .......... According to the answer of r0... i asked why does this ... anyway the designed was wrong ;)

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by dmjpro
                    Don't get me wrong .......... According to the answer of r0... i asked why does this ... anyway the designed was wrong ;)
                    Erm, yes, that's what I was trying to tell you all the time in replies #2, #4 and #8, but glad you understand it now.

                    kind regards,

                    Jos

                    Comment

                    Working...