Tuesday, September 8, 2009

EJB and Hibernate notes

EJB

*Session Bean -- stateful and stateless
* Entity Bean
* Message Driven Bean

EJB Architecture
* A remote interface -- a client interacts with it
* A home interface -- used for creating objects and for declaring business methods
* A bean object -- an object which actually performs business logi cna EJB-specific operations
* A deployment descriptor -- ( an XML file containing all information required for maintaining the EJB) or a set of deployment desciptor.
* A primary key class - is only entity bean specific.

[http://www.comptechdoc.org/docs/kanti/ejb/ejbarchitecture.html]

Entity Bean
* represent data in the database.
* most of the time container takes care of the persistence, transaction and access control while developer can focus on the bean logic.

Container Managed Bean
* all the logic for synchronizing the bean's state with the database is handled automatically by the container.
* developer does not need to write any data access logic.
* container takes care of persistence.

Bean Managed Persistence
* developer can handle the persistence if needed.
* developer can handle the data source.

ORM
ORM is the automated persistence of objects in Java application to the tables in a relational database.

Hibernate
*persistence framework
*pure java object relational mapping.
*maps POJO to relational database tables.
*relieves developr from persistence related programming task.
*hibernate.cfg.xml - hibernate configuration file
* *.hbm.xml -- mapping file, tells which tables and columns to use to load and store objects.
*hibernate.properties - resource bundle

session
*light-weight and non-threadsafe object
*sessionFacotry creates session and then closes it once work is done.

Hibernate Persistent Class (Hibernate Bean)
* just another regular bean class.
* setters and getters, no arg constructor
* can override the equals() and hashcode() methods.
* the persistent class should not be final.

JDBC and Database related notes

JDBC Connection steps

* loading driver class - Class.ForName("driver class").newInstance();
* Connection - Connection con = DriverManager.getConnection("databaseurl","username","passwprd");
* statement - Statement stmt = con.CreateStatement();
* execute sql - stmt.executeQuery("query");
* get data from resultset - rst.getString("column_name");

Auto Commit

con.setAutoCommit(false);
.........................
.........................
(transaction statements)
.........................
.........................
con.setAutoCommit(true);

Calling Stored Procedure



CallableStatement cs = con.prepareCall("call show_supplier");
Resultset rs = cs.executeQuery();  
Warnings(SQL)

To handle sql warning connection, statement, resultset each object can invoke a method called getwarning. [con.getWarning();]

JDBC driver types

Type1:- JDBC-ODBC bridge
Type2:- Native API partly Java Driver
Type3:- Network Protocol Driver
Type4:- JDBC Net pure Java Driver

Logging JDBC

DriverManager.println();

SQL Locator
A locator is a SQL3 datatype that acts as a logical pointer to data. Used to handle- array, blob and clob data.

Transaction Isolation Level
* 4 levels
* Connection.setIsolationLevel()
* Isolation Levels
TRANSACTION_READ_UNCOMMITTED
TRANSACTOIN_READ_COMMITTED
TRANSACTION_REPEATABLE_READ
TRANSACTION_SERIALIZABLE

Anomalies
* Dirty Reads
* Non-repeatable reads
* Phantom reads

Metadata
* two important classes : DatabaseMetaData and ResultsetMetaData
DatabaseMetaData.getImportedKeys() returns a resultset with data about foreign keys etc.


Locking
Pessimistic Locking: - good for data integrity, bad for concurrency, defensive approach, lock the data and always expect that someone can access the interim data before it gets updated.

optimistic Locking:- exptects that a clash between multiple updates to the same data will seldom occur.

batch updating
stmt.addBatch(SQL statment here);
stmt.addBatch(SQL statment here);
stmt.addBatch(SQL statment here);
................................
................................
stmt.executeBatch();
stmt.clearBatch();