Solved

JSP db Connection design question

Posted on 2004-10-06
7
293 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Are you unable to connect or configure Hotmail email account in Microsoft Outlook 2010, 2007? Or Outlook.com emails are not downloading to Outlook? Lets’ see the problem and resolve Outlook Connector error syncing folder hierarchy (0x8004102A).
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now