Closing Database issue

Discussion in 'Java' started by francan00@yahoo.com, Oct 16, 2007.

  1. Guest

    I have a method that inserts data into my Oracle 9i database with no
    problems or Database closing issues:

    CODE
    public class MainClass
    {

    public PreparedStatement preparer;
    public Connection connection;

    public MainClass()
    {
    connection = new DbConnectionClass().getConnection();
    }

    public int inserter(Beann abc)
    {
    int dat = 0;
    try
    {
    preparer = connection.prepareStatement("insert into abTable
    (one,two) values (?,?)");
    preparer.setString(1, abc.getOne());
    preparer.setString(2, abc.getTwo());
    preparer.executeUpdate();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    return dat;
    }

    public int matcher(Beann abc)
    {
    try
    {
    inserter(abc);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    finally
    {
    //close the ResultSet ....
    //close the Statement ....
    //close the Connection ....
    }
    }

    Now when I put the method (inserter) in another class called
    OtherClass, it does insert the data but now I have database closing
    issues:


    CODE
    public class OtherClass
    {
    ....
    public int inserter(Beann abc)
    {
    int dat = 0;
    try
    {
    preparer = connection.prepareStatement("insert into abTable
    (one,two) values (?,?)");
    preparer.setString(1, abc.getOne());
    preparer.setString(2, abc.getTwo());
    preparer.executeUpdate();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    return dat;
    }
    .....



    CODE
    public class MainClass
    {

    public PreparedStatement preparer;
    public Connection connection;

    public MainClass()
    {
    connection = new DbConnectionClass().getConnection();
    }

    public int matcher(Beann abc)
    {
    try
    {
    new OtherClass().insert(abc);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    finally
    {
    //close the ResultSet ....
    //close the Statement ....
    //close the Connection ....
    }

    In Oracle SQL Plus database resource check I see JDBC Thin Client is
    opened and not closed after each insert with the above attempt.
    This didnt happen when I had the method in the same class.

    Please advise.
     
    , Oct 16, 2007
    #1
    1. Advertising

  2. wrote:
    > I have a method that inserts data into my Oracle 9i database with no
    > problems or Database closing issues:
    >
    > CODE
    > public class MainClass
    > {
    >
    > public PreparedStatement preparer;
    > public Connection connection;

    ....

    Your inserter method updates preparer to point to the prepared statement.

    When you move the insert to OtherClass, does preparer in the MainClass
    object still get updated? Does the finally block depend on preparer in
    MainClass pointing to the prepared statement?

    Patricia
     
    Patricia Shanahan, Oct 16, 2007
    #2
    1. Advertising

  3. Guest

    On Oct 15, 7:47 pm, Patricia Shanahan <> wrote:
    > wrote:
    > > I have a method that inserts data into my Oracle 9i database with no
    > > problems or Database closing issues:

    >
    > > CODE
    > > public class MainClass
    > > {

    >
    > > public PreparedStatement preparer;
    > > public Connection connection;

    >
    > ...
    >
    > Your inserter method updates preparer to point to the prepared statement.
    >
    > When you move the insert to OtherClass, does preparer in the MainClass
    > object still get updated? Does the finally block depend on preparer in
    > MainClass pointing to the prepared statement?
    >
    > Patricia


    I assume you are asking if preparer in MainClass updates the database
    using the insert from OtherClass. Yes it does update the database but
    leaves the database open in some way. If I put the finally block
    close statements in OtherClass it wont update the database because it
    seems to close the connection/statement before it gets called in
    MainClass.
     
    , Oct 16, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    655
    Thomas Hansen
    Feb 15, 2007
  2. Replies:
    0
    Views:
    278
  3. Replies:
    0
    Views:
    313
  4. Evans
    Replies:
    1
    Views:
    272
    Evans
    Jun 28, 2008
  5. thomas
    Replies:
    0
    Views:
    229
    thomas
    Oct 23, 2003
Loading...

Share This Page