Static methods of Data Access Object in stateless session bean

Discussions

EJB programming & troubleshooting: Static methods of Data Access Object in stateless session bean

  1. Hi,

    I am using Data Access Object pattern with stateless session bean to decouple business logic from data access logic. All methods (including methods for inserts and updates) of my ADO's are public and static. Will these static method will be safe to handle concurrency issue (when one then one cliect access the same method at same time)

    Thanks
    Sabir

  2. It will be ok to use if the connection is passed on to each static method of the DAO from the session bean. ( I am assuming there are no other static attributes for DAO).
  3. Thanks Bhagvan,

    But how sending the connection object will resolve this problem????

  4. Connection:
    That's one of the attributes which comes into mind for a DAO. Other than that, most of the time all are passed to the methods of a DAO. If you can pass the connection as a parameter, the method is like any other util class method.

  5. To go one step further to explain the issue that you must be aware of when using static methods, what you actually need to ensure is that your DAO is thread safe. As a general rule of thumb each method should only use data passed as a parameter (e.g. connection) and should not utilise member data. Sometimes you need to bend this rule but you should only access member data that is pertinent to the class (e.g. a reference to a thread safe logger) and not the client.

    HTH

    P.
  6. It is more desirable to build the DAO as a Class with constructors and public method, etc. If you are using connection pooling, in your constructor you'd do a JNDI lookup of the DataSource object and stores it as a member variable. For each DAO method in this class you'd do something like this:

    javax.sql.DataSource dataSource;

    public void doXX(....) {
       try {
       connection c = dataSource.getConnection();
       //do you JDBC calls ....
       }
       catch (SQLException e) { .......}
       finally {
         try {c.close();}
         catch (Exception e2) { ..}

       }



    }

    Create a DAO instance for each Stateless Session Bean.
  7. Hi

    Why you want to use static method?

    I am making instance for every bean.

    Use code as follow :



        try
            {
            String lsDelete = "delete from CI_FAILED_KEYS where INTERNAL_ID = ?";

    //This will look up data source from Context abd Give u connection.
            con = ARDBManager.getConnection();
            ps = con.prepareStatement(lsDelete);

            ps.setString(1, foPk.INTERNAL_ID);

            ps.executeUpdate();
        }
        catch (SQLException sqle)
            {
            sqle.printStackTrace();
            throw new RemoveException("Error Occured while Deleting the Record :" + foPk.toString());
        }
        finally
            {
            try
                {
                if (ps != null)
                    {
                    ps.close();
                }
                if (con != null)
                    {
                    con.close();
                }
            }
            catch (SQLException sqle)
                {
                System.out.println("Error Occured while Closing the Connection :" + sqle.getMessage());
            }
        }
    }
  8. Data Access Object code example[ Go to top ]

    You can use FireStorm/DAO (free download) to generate Data Access Object (DAO) code from your database. It's a great way to learn about Data Access Objects.

    http://www.codefutures.com/products/firestorm/