Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Closing Database issue

Thread view: 
francan00@yahoo.com - 16 Oct 2007 00:41 GMT
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.
Patricia Shanahan - 16 Oct 2007 00:47 GMT
> I have a method that inserts data into my Oracle 9i database with no
> problems or Database closing issues:
[quoted text clipped - 5 lines]
> 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
francan00@yahoo.com - 16 Oct 2007 01:57 GMT
> franca...@yahoo.com wrote:
> > I have a method that inserts data into my Oracle 9i database with no
[quoted text clipped - 16 lines]
>
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.