Solved

JSP db Connection design question

Posted on 2004-10-06
7
299 Views
Last Modified: 2010-08-05
Hi,

I am planning on using just JSP and Struts for my application that has an Oracle backend. I don't want to use JavaBean orDAOs because of the short timeframe. I want to know the best way to implement the database connections logic. Should I have  a database utilitly class that has getConnection and close connection methods and call this class directly in JSP when ever there is need to query or update? The appliction will run on Weblogic so I have to configure the datasource in weblogic.

Thanks in advance
Dasari
0
Comment
Question by:DASARI_R
7 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 12236352
> Should I have  a database utilitly class that has getConnection and close connection methods and call this class directly in JSP when ever there is need to query or update?

Yes :-)
0
 

Author Comment

by:DASARI_R
ID: 12236503
Can you please give me some design ideas or an example ie where to use static method or a singleton class

class DBMgr()
{
Connection conn;    
public static Connection getConnection()
{
 // do init
return conn;
}

public static void close()
{
if (null != conn)
  conn.close();
}

}
DBMgr.getConnection()
DBMgr.close()

If I use this in JSPs will there be a synchonization issue or will every client get a new connection ?

Thanks
Vinay
0
 
LVL 3

Expert Comment

by:applekanna
ID: 12243621
Hi DASARI_R,
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html

Just do "connection pooling java example" in google and you will find all that you want

Cheers!
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Accepted Solution

by:
kokchoon78 earned 125 total points
ID: 12257056
I think struts provides a simple Database Pooling..Here is how i implement :

In my struts action class, i get the datasource object with :

DataSource dataSource = getDataSource(request);

and then pass the datasource object to my Database Manager class.

For example :

PageDA pageDA = new PageDA();
pageDA.deletePage( dataSource, pageId );

in th PageDA class :

public boolean deletePage( DataSource dataSource , int pageId)
{
   Connection myConnection = null;
   PreparedStatement pstmt = null;
   boolean returnB = false;

   try
   {
     myConnection = dataSource.getConnection();

     pstmt = myConnection.prepareStatement("delete from PAGE where PAGE_ID=?");
     pstmt.setInt( 1, pageId );
    if( pstmt.executeUpdate() == 1 )
    {
       returnB = true;
     }

     if( !myConnection.getAutoCommit() )
     {
       myConnection.commit();
     }
   }
   catch (SQLException sqle)
   {
     try
       {
         myConnection.rollback();
        }catch(Exception e)
       {
          e.printStackTrace();
       }
       sqle.printStackTrace();
    }
    finally
    {
       try
       {
           if( pstmt != null ) pstmt.close();
           if( myConnection != null ) myConnection.close();
        }
       catch (SQLException e)
       {
           e.printStackTrace();
       }
    }
return ( returnB );
}

hth,
Kok Choon.

0
 
LVL 35

Expert Comment

by:TimYates
ID: 12502483
I think it should be:

Split:  applekanna {http:#12243621} & kokchoon78 {http:#12257056}

:-)
0
 

Author Comment

by:DASARI_R
ID: 12503997
Kok Choon,

Thank you for the answer.

Dasari
 
 
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java beans and EJBs 5 196
bean mapper frameworks 5 63
project group option in netbeans equivalent term in eclipse 1 51
java operators 3 120
If you are looking at this article, you have most likely been hit by some version of ransomware and are trying to find out if there is anything you can do, or what way you should react - READ ON!
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question