0

Writing a program to determine management level ID's from a database. I've used this same connection method through the site over and over but it doesn't hang anywhere else. However, those methods aren't buried within an actual class. Is there any reason why putting this particular code into a java class would make it hang?

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import oracle.jdbc.driver.OracleDriver;
import java.sql.*;

public class connection 
{  

  public Connection getDBConnection()
  {
     java.sql.Connection conn=null;

     try
     {
        DriverManager.registerDriver(new OracleDriver());
        conn = DriverManager.getConnection("stuff"); //Hangs here for like 2 seconds

     }
        catch (Exception e)
        {
           e.printStackTrace();
        }

     return conn;       
  }
}

Just to reiterate, I use this via a straight up jsp (no classes in that jsp) throught the site, but for this particular segment, I implemented it as a method within a class. This seems to be the only place where it hangs. Also, if you could give me the specific imports to make the database connection work so I can get rid of some of those ".*"...that would be awesome. Thanks

Edit: For more clarity, I create object X and in object X it creates the connection class object which calls this method.

So,

 someClass whatever = new someClass(); 
 Connection conn = null; 
 conn = whatever.getDBConnection(); //hangs
4
  • Just some best practice thoughts - Class names should begin with a capital letter (Connection); You only need to import java.sql.DriverManager, and oracle.jdbc.driver.OracleDriver; Change your return type to java.sql.Connection if you adjust your class name; Database connections from the JSP should be avoided in most cases... you should have a data access layer that does this. Commented Mar 30, 2012 at 15:27
  • but I thought jsp's WERE a data access layer. Bah, I really wish I knew someone who knew this stuff. Commented Mar 30, 2012 at 15:52
  • @cphilpot I just approved your suggested edit to MrJames's answer. Note that the info is still available in the revision histories of the posts it was in. Usually, nothing gets permanently deleted on SO, but I flagged for a mod to see if they can purge it from the DB for you in this case. Commented Mar 30, 2012 at 20:12
  • Better yet, just change the password. Commented Mar 30, 2012 at 20:29

2 Answers 2

1

Are you pooling the connections in other places? If you are creating the new connection each time then it could likely take a couple seconds.

Sign up to request clarification or add additional context in comments.

9 Comments

It's on the homepage, so I've created exactly 1 connection before this is called within the class. So I don't know why it would be a connection pooling issue. Though, I'm still new to this.
You should probably be using a data source inside your web app instead of directly using the driver manager.
If I knew what that meant, I'd be all over it. Mind explaining how I would go about that?
Well it depends on which container you are using, but if it's something like jboss, just lookup on how to add data sources to the container then you can look them up with simple JNDI calls.
Ok, so I'm using a tomcat server (container, whatever) and I've been using that method to get connections. From what you've said and what I've found, this is bad. How do I use the tomcat container to get the connection instead?
|
1

Also, why are you always registering the OracleDriver?

You should only register the driver once.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import oracle.jdbc.driver.OracleDriver;
import java.sql.*;

public class connection 
{  
  static {
      try {
         DriverManager.registerDriver(new OracleDriver());
      }
      catch (SQLException excep) { // log exception }
  }


  public Connection getDBConnection()
  {
     java.sql.Connection conn=null;

     try
     {
        conn = DriverManager.getConnection("stuff"); //Hangs here for like 2 seconds

     }
        catch (Exception e)
        {
           e.printStackTrace();
        }

     return conn;       
  }
}

2 Comments

If you are running inside a web container you should use a DataSource (through JNDI) that was previously configured through the web container administration console. PS: I thought that you wanted to run this code on a single Java class application
Right. When you are using a DataSource you don't need to worry about db configurations (ip, user, etc) you just need to get data source 'X'. Who administers the web container will define that configurations for you; he will define that DataSource 'X' implies ip:y, user:z... and so on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.