I have a question / need help understanding how to configure backing bean and model objects so that memory and object creation/deletion is done as efficiently as possible. 

 

1.  I have a .jsf page with a form and a commandbutton that submits the form inputs to a backing bean (enrollispbean is backing bean)

<h:commandButton value="Enter" action=""

 

2.  The backing bean is used for form handling - the insert() method is used to read the data fields from the form and create a SQL string that will be submitted to a model object, DbInsert, that is used as a generic data access object that connects to the database and insert the SQL string:

 

public class EnrollIspBean {

   private String beanvar1="";

   private String beanvar2= "";

//  DbInsert is data access object

   private DbInsert dbinsert = new DbInsert();

 

 public String insert (){

     String sqlstmt;

    sqlstmt = "INSERT INTO ispmain VALUES(beanvar1, beanvar2,..)"

     dbinsert.insert(sqlstmt);

     return "success"; }

 

3.  DbInsert is the data access object that contains a method, insert(), that accepts a sql string to insert into the database.  This method contains the code to obtain a connection from the database connection pool and then execute the sql statement (note: error checking code not shown):

 public class DbInsert {

 

  public void insert(String sqlstmt) throws SQLException {

  Connection conn = null;

  GetDBConnection getdbconnection = new GetDBConnection();

  PreparedStatement stmt = null;

 

  conn = getdbconnection.getdbconn();

  stmt = conn.prepareStatement(sqlstmt);

  stmt.executeUpdate();

  stmt.close();

  conn.close();

  return;

 }

 

Where I need help understanding is how to set up the scope for the managed beans and data access object.   Currently, I have the backing bean within the session scope (using the facesconfig.xml file).  My main question is how to set up the scope for the Data Access Object - currently I do not have it as a managed bean within facesconfig.xml.  Instead I am creating a new instance within the backing bean:

 

private DbInsert dbinsert = new DbInsert();

 

Is this the best way to do this?  Will the DBInsert object now be tied to the session scope of the backing bean (i.e., when backing bean is deleted, the DbInsert object will be deleted from session scope as well.) 

 

Ideally I would like the data access object to be available as a shared object throughout the life of the application.  When I was programming using a servlet approach, I would have created a servlet to load on startup.  Now that I'm using java server faces, I'm confused about the scope / how to efficiently  set up a data access object that I want to be available to all backing beans in the application.

 

tnanks for any help understanding this.

 

Tom

 

Reply via email to