I'm new at EJB development and i'd like to know if i don't need to use DaO pattern with CMP entity beans or i can't use it, and why.
If i use DaO pattern to access data on a table, and use CMP entity beans to update this table, i have chance of read outdated data?
if you arae going to USE CMP u need not be bothered of retreiving or inserting data to DB.The container takes care of it.All u have to do is in the deployment descriptor map your EJB fiedl to the exact Column in DB.
DAO is for BMP
i know that. But i just want to know why i can't use Dao pattern for data retrieving and cmp entity bean for updates, both called from the same session bean.
Maybe you mean the "Data Object pattern" (AKA "Value object pattern", "Details object pattern")?
The Value Object pattern facilitates the use of a Java object that reflects all the entity bean's data, so that it can be retrieved in a single call (and updated in a single call). Is that what you ment? If so, yes, you can use it with CMP beans.
the DAO pattern, Data Access Object, facilitates the use of a special object that is responsible for reading/writing data from the DB (or any other resource). This pattern is meaningless in the CMP context, because with CMP you don't access the DB (or other resource) yourself.
I am really talking about DaO pattern.
To be more clear, i am thinking about make a session facade and this facade acesses the DaO object to make selects and the facade accesses the CMP entity to make delete/insert/update. The reason i'm thinking about it is the fact that DaO object uses direct JDBC access to the database, out of container control, leading, i think, to a better performance on data retrieving, and i still have transaction control by container to the other tasks. If i put the DB access inside the session bean, it will be db dependent, what i don't want. the point is when multiple users acess this session at the same time, some making updates and others making selects, i am affraid of facing the problem of be reading outdated data, cause i can make a DaO call between
another user ordered an update and the container efectively does this update. Is it true?
As I understand your question, you are not talking about using a DAO within a CMP entity, but rather as an alternative to CMP for performing certain kinds of data access.
This is indeed possible, and not very uncommon. Some times accessing the data through CMP simply doesn't cut it: too much data, very complex queries, dynamic queries, etc. In these cases the approach you describe is relatively simple, and if you consistently use Value objects it shouldn't have that big an impact on your existing code.
The container uses DBs transaction infrastructure: transactions are safe across the DB, not across a specific container or CMP bean. So you shouldn't have serious problems in that respect.
Accessing the data directly in this manner can be faster on some cases, but it can also be slower, especially if the data is mostly read-only (and read-only transactions are supported by the container). I personally prefer to avoid the approach you mentioned unless there is a special reason to use it: I don't think it should be used as a "default".
If you use the DAO pattern for read queries, there are very remote chances that you will be getting outdated/obsolete data as every read call has to go back to the database. How ever This wont be the case in entity bean also because the bean's state will change before any update of a record is carried out. the problem could arise when you read and update the data. After the read and before update some one else may change the data.
This is a vey common problem and may occur irrespective of the use of DAO or CMP or combination of both.