Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

JSP db Connection design question

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
DASARI_R
Asked:
DASARI_R
1 Solution
 
TimYatesCommented:
> 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
 
DASARI_RAuthor Commented:
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
 
applekannaCommented:
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
Technology Partners: 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!

 
kokchoon78Commented:
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
 
TimYatesCommented:
I think it should be:

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

:-)
0
 
DASARI_RAuthor Commented:
Kok Choon,

Thank you for the answer.

Dasari
 
 
0

Featured Post

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now