Welcome to Dream.In.Code
Become a Java Expert!

Join 149,580 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,753 people online right now. Registration is fast and FREE... Join Now!




Database closing issue

 
Reply to this topicStart new topic

Database closing issue

Rating  5
kensington
15 Oct, 2007 - 05:23 PM
Post #1

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 31


My Contributions
I have a method that inserts data into my Oracle 9i database with no problems and no 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 using try/catch
        //close the Statement using try/catch
        //close the Connection  using try/catch
     }
}


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 int matcher(Beann abc)
{
     try
     {
         new OtherClass().insert(abc);
     }
     catch(Exception e)
     {
         e.printStackTrace();
     }
     finally
     {
        //close the ResultSet using try/catch
        //close the Statement using try/catch
        //close the Connection  using try/catch
     }


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.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Database Closing Issue
16 Oct, 2007 - 08:37 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,277



Thanked: 135 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
It probably depends quite a bit on what's going on with this call.
CODE
DbConnectionClass().getConnection();


Now you are making that call in two places instead of one, so they could be steping on each other.

Try passing the current conection to the other class, rather than making is responsible for it's making it's own.

CODE
public class OtherClass {
   private Connection connection;
   public OtherClass(Connection connection){ this.connection = connection; }
...
}


Ideally, your getConnection() method makes a distinctly different connection object on each call, but who knows.

Hope this helps.
User is offlineProfile CardPM
+Quote Post

kensington
RE: Database Closing Issue
16 Oct, 2007 - 02:53 PM
Post #3

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 31


My Contributions
Thanks, it now works great with no database leaks!

If I understand it correctly I had two connections and now with the way you showed me I have just one connection? And if I have 2 more classes also needing database connection I can use the same code for each class?

CODE

public class SecondClass {
   private Connection connection;
   public SecondClass(Connection connection){ this.connection = connection; }
...
//DB stuff such as inserts, queries and updates..
}


CODE

public class ThirdClass {
   private Connection connection;
   public ThirdClass(Connection connection){ this.connection = connection; }
...
//DB stuff such as inserts, queries and updates..
}

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Database Closing Issue
16 Oct, 2007 - 04:03 PM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,277



Thanked: 135 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Yes, passing the connection to all other objects would work. At that point, you might consider a base object. Note, this is not the best design. You might do well with a single connection point that everyone references. Again, not knowing the guts of DbConnectionClass, we can just use a wrapper.

In this case, ConnectionManager will be responsible for holding the connection. Future versions could be streamlined to use a connection pool or something. This pattern begs to be a Singleton, but that's a longer post.

CODE
public class ConnectionManager {
    private static Connection activeConnection = null;
    public static Connection getConnection() {
        if (activeConnection = null) {
            activeConnection = new DbConnectionClass().getConnection();
        }
        return activeConnection;
    }
}


If every instance just references ConnectionManager.getConnection(), then you have a single point of connect that shouldn't step on itself. No need to pass it around.


User is offlineProfile CardPM
+Quote Post

kensington
RE: Database Closing Issue
16 Oct, 2007 - 05:04 PM
Post #5

New D.I.C Head
*

Joined: 8 Oct, 2007
Posts: 31


My Contributions
Thanks!

Please advise if this is where the Driver and DB username and password would go and look like?

DbConnectionClass???
CODE

public class DbConnectionClass
{
   public DbConnectionClass()
   {
     try
     {
        Class.forName("OracleDriverInfoHere");
         Connection connection = DriverManager.getConnection("dbURLHere", "usernamehere", "passwordhere");
       }
       catch(....
       {
             //catch info here...
        }
    }
}


ConnectionManager:
CODE

public class ConnectionManager
{
     private static Connection activeConnection = null;
     public static Connection getConnection()
    {
        if (activeConnection = null)
        {
           activeConnection = new DbConnectionClass().getConnection();
         }
         return activeConnection;
    }
}


User is offlineProfile CardPM
+Quote Post

stephaniecoleiro
RE: Database Closing Issue
21 Oct, 2007 - 08:38 AM
Post #6

New D.I.C Head
*

Joined: 21 Oct, 2007
Posts: 1


My Contributions
QUOTE(kensington @ 16 Oct, 2007 - 06:04 PM) *

Thanks!

Please advise if this is where the Driver and DB username and password would go and look like?

DbConnectionClass???
CODE

public class DbConnectionClass
{
   public DbConnectionClass()
   {
     try
     {
        Class.forName("OracleDriverInfoHere");
         Connection connection = DriverManager.getConnection("dbURLHere", "usernamehere", "passwordhere");
       }
       catch(....
       {
             //catch info here...
        }
    }
}


ConnectionManager:
CODE

public class ConnectionManager
{
     private static Connection activeConnection = null;
     public static Connection getConnection()
    {
        if (activeConnection = null)
        {
           activeConnection = new DbConnectionClass().getConnection();
         }
         return activeConnection;
    }
}



Hi, i'm new in Java and my teacher have given us a project. We have to do a database with Java but i don't have any idea of how to start it. Can someone give me an idea please? Thankx.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month