While using container managed entity bean when is the bean stored to database by the container?
Can this be controlled inside the servlet using some code statements or the container manages this automatically internally?
While using container managed entity bean when is the bean stored to database by
Share
Basically it is stored to the database when your transaction is committed.
With CMP you can control this with transaction demarcation in the deplyment descriptor a’la
This is a snippet from the spec http://download.oracle.com/otn-pub/jcp/ejb-2.1-fr-spec-oth-JSpec/ejb-2_1-fr-spec.pdf
When you invoke a method on an ejb a new transaction may be initiated by the container, depending on your demarcation, and if the method terminates successfully, the transaction may be committed, depending on this-and-that. For instance if your specific method was demarcated with requires-new, it will be committed, because it has it’s own transaction, but if it is demarcated with requires, and was called within an aready running transaction (for instance because it was called from a session bean method demarcated with, say, requires or requires-new), it will be committed when that enclosing transaction is committed. Or if it fails, not only the changes made inside that method will be rolled back, but also changes made inside the enclosing method call/transaction.
While the deployment descriptor may allow for nested transactions, it actually boils down to how the database engine handles transactions in the end. Very often rdbms doesn’t really support nested transactions, but instead uses transaction savepoints, so what appears as recursive from the ejb-perspective may actually happen in a “linear” fashion in the database. In most scenarios the result is the same, though.
It may also be relevant to consider transaction isolation level.