Solved

JSP db Connection design question

Posted on 2004-10-06
7
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The recent Petya-like ransomware attack served a big blow to hundreds of banks, corporations and government offices The Acronis blog takes a closer look at this damaging worm to see what’s behind it – and offers up tips on how you can safeguard your…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

623 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