jboss at work databases and jboss chapter 4 jeff schmitt october 26, 2006

12
JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

Upload: alexia-nicholson

Post on 03-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

JBoss at WorkDatabases and JBoss

Chapter 4Jeff Schmitt

October 26, 2006

Page 2: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

Persistence Tier

• ORM – Object Relational Mapping

• Frees the developer from the perils of converting ResultSets to ArrayLists of DTOs

• Allows the database to be “transparent” (don’t see it)

• Therefore we call this the Persistence Tier rather than the Database Tier

Page 3: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

JDBC

• According to Sun, not an acronym, but strongly suggests “Java Database Connectivity”

• An API that loads database drivers, makes connections, create statements, that yield ResultSets upon execution

• You write the SQL: Insert, Update, Delete, etc.• OO purists do not like the thought of dealing with

strings and converting to/from objects• Violates DRY principle - Don't Repeat Yourself –

maintains two representations – Java and SQL

Page 4: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

OODBMS

• Object Oriented DBM systems exist• Removes the need to marshal and unmarshal

objects• Unmarshaling, in data binding terms, refers to

the conversion of XML documents to Java object instances.

• OODBMS are not popular, low market share• Relational DB systems are widely accepted --

entrenched

Page 5: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

EJB Entity bean

• Entity beans – two basic variations– BMP – Bean managed persistence– CMP – Container managed persistence

• Problem with EJB:– Not a simple POJO: you are forced to deal

with a variety of complicated and interdependent classes

– Objects tightly coupled with container: you are forced to inherit from EJBObject and implement specific intergfaces

Page 6: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

JDO

• Java Data Objects – Sun’s next attempt at a JavaBean-centric persistence API

• Not an official part of J2EE standard• RDBMS vendors slow to support it• OODBMS vendors enthusiastically support

it• Main problem is a complicated API –

different from Entity Beans but still complicated

Page 7: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

Hibernate ORM

• JBoss Hibernate – one of the most popular of the ORMs

• JBoss supports it• Seems to best represent what the next

generation of persistence API’s will look like

• Meanwhile Sun is working toward a merger of EJB 3.0 and JDO 2.0 – it may resemble Hibernate (we’ll have to see)

Page 8: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

Case Study

• Chapter 4: JDBC version

• Chapter 5: Hibernate version

• Both chapters use a DAO to hide the details of the persistence layer

• We create a common interface: CarDAO

• Method: public List findAll()

Page 9: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

JDBC Disadvantages

• Every connection to the db must incur overhead

• You have to manage the database transaction yourself

• If you combine DB activity and non-DB such as JMS messages, No way to roll back transactions – this is called a global transaction

Page 10: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

DataSource advantages

• JBoss creates a Connection Pool at startup – when you get or close a database connection, you just access an open connection from the pool

• Container-managed Transaction Context – commits or rolls back automatically

• Can participate in a global transaction – if you combine DB activity and JMS messages, Transaction Manager can roll back everything

Page 11: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

JNDI – two level names

• ENC - Environmental Naming Context – highly recommended naming convention for data sources

• JNDI name begins with java:comp/env• web.xml gives a resource-reference name --

local to the EAR (jdbc/JBossAtWorkDS)• jboss-web.xml maps the resource-reference to

the global JNDI name Source (java:JBossAtWorkDS)

• We use XDoclet to generate our web.xml file– See ControllerServlet.java

Page 12: JBoss at Work Databases and JBoss Chapter 4 Jeff Schmitt October 26, 2006

Hypersonic DB

• Hypersonic completely implemented in Java. Uses JDBC. Ships standard with JBoss

• A local db where modifications are stored to disk• A variation allows hypersonic to run purely in memory

(efficient)• Can be configured to listen on a TCP port like a

database server• JCA – J2EE Container Architecture – a standard way for

a J2EE container to connect to external datastores• See Jaw-ds.xml• Place hsqld.jar -- $JBOSS_HOME/server/default/lib – to

share across multiple EARs