Unit 3 - Java Database Connectivity

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

JBoss AS Administration

Unit 3 – Java Database Connectivity (JDBC)


Objectives

• At the end of this module you will be able to:


– Describe the high level architecture of JDBC
– List the four driver types and those provided by
JBoss
– Describe and configure connection pools
– Describe and configure DataSources and
Transactional DataSources
– Use the Administration Console to manage JDBC
resources
Road Map

1. Overview of JDBC
– High level architecture of JDBC and the driver
model
– Four different driver types
– Differences between 2-tier and multitier models
2. Connection Pools
3. DataSources and Transactional
DataSources
What is JDBC?

• JDBC is an API for accessing databases in a uniform


way.
• JDBC provides:
– Platform independent access to databases
– Location transparency
– Transparency to proprietary database issues
– Support for both two-tier and multitier models
for database access
JDBC Architecture
Type 1 Driver

• The Type 1 driver:


– Is a JDBC-ODBC bridge
– Usually runs on Windows
– Requires ODBC driver to be installed on client machine
Type 2 Driver

• The Type 2 driver:


– Requires a native driver to be already installed on the
client machine
– The driver converts JDBC calls to native API calls of the
database
Type 3 Driver
… Type 3 Driver

• A network server can apply several techniques to


boost performance:
– Load management
– Caching
– Pooling
Type 4 Driver

• Type 4 drivers are ‘all-Java’ driver implementations.


• They do not have extra layers between the client
and the database.
Two Tier Architecture

• In the two-tier model a Java application communicates directly


with the DBMS.
• A JDBC driver is needed that can communicate directly with
the DBMS.
• This is a client/server configuration.
Multi Tier Architecture

• In the multi-tier model, commands are sent to


a"middle tier" of services, which then sends
the commands to the DBMS.
• The DBMS processes the commands and sends
the results back to the middle tier, which
then sends them to the client.
Choosing The Correct Driver

• Choosing the correct driver can have significant


impact on performance.
• For two-tier applications use the type 1, 2 or 4
driver specific to the DBMS you are using.
• For multi-tier applications use:
– The RMI driver (DataSources) in a client class a type 1, 2 or
4 driver on the server, specific to the DBMS you are using
– The JTS or Oracle XA driver in EJB or where transaction
support is required
Section Review

• In this section we discussed:


– The high level architecture of JDBC
– Four different driver types
– Two-tier v. multi-tier architectures
– What driver types should be used for your application
Road Map

1. Overview of JDBC
2. Connection Pools
– Connection Pools and How they work
– Advantages of Using Connection Pools
– Creating and configuring Connection Pools
3. DataSources and Transactional
DataSources
What is a Connection Pool

• A connection pool is a group of ready-to use


database connections.
• Connection pools:
– Are created at JBoss AS startup
– Are managed using the Administration Console
– Can be dynamically resized to accommodate increasing load
How Connection Pools are used?

• A connection pool allows an application to


“borrow” a DBMS connection.
Benefits of Connection Pools

• Some advantages of using connection pools are:


– Time and overhead are saved by using an existing database
connection
– Connection information is managed in one location in the
Administration Console
– The number of connections to a database can be controlled
– The DBMS can be changed without the application
developer having to modify underlying code
Connection Pools Checklist

• Before creating a connection pool, determine:


– What driver to use and its class name
– The JDBC URL of the database
– A set of connection properties used to authenticate a user
or optionally configure the driver

• Ask your DBA for:


– The maximum number of connections your application will
be allowed
– A database account (username/password) that your
application will be able to use
JDBC URLs

• Database locations are specified using a JDBC


Uniform Resource Locator (URL). JDBC URL is
created by the JDBC Connection Pool Assistant.
JDBC URLs Examples
Connection Properties

• Connection properties are:


– Key/value pairs used to configure JDBC connections and are
passed to the driver during connection setup
… Connection Properties

• A partial list of connection properties for the


supplied drivers:
Configuring the Statement cache

• The statement cache types are as follows:


– Least-recently used (LRU): after the statementCacheSize
(default:10) is met, the least-recently used statement is
removed when a new statement is used.
– Fixed: the first statementCacheSize (default:10) number of
statements is stored and stays fixed in the cache. New
statements are not cached unless the cache is manually
cleared or the cache size is increased.

<share-prepared-statements> - with prepared


statement cache enabled whether two requests in
the same transaction should return the same
statement (from jboss-4.0.2 - default false)
Section Review

• In this section we discussed:


– What connection pools are and how they work
– The advantages of connection pools
– Using the Administration Console to create and
manage connection pools
Road Map

1. Overview of JDBC
2. Connection Pools
3. DataSources and Transactional DataSources
– Describe a DataSource and how it works
– Describe a transactional DataSource
– Use the Administration Console to create a DataSource or
transactional DataSource
– Configure JDBC Logging
What is a DataSource?

• A DataSource object provides a way for a JDBC


client to obtain a database connection from a
connection pool.
• A DataSource:
– Is stored in the WLS JNDI tree
– Contains a reference to a connection pool
– Can support transactions
– Must be created for each individual connection pool
How DataSources are used?

• A client retrieves a DataSource through JNDI look up


and uses it to obtain a database connection.
What is a Tx Data Source?
• A Tx Data Source:
– Is created when Honor Global Transaction flag is true
– Enables support for JTA
– Is required for distributed Transactions
Enable Global Transactions in a Data Source

• A Tx Data Source:
– Enables support for JTA
– Is required for distributed Transactions
• When to enable global transactions:
– Use the Java Transaction API (JTA)
– Use the EJB container in JBoss Server to manage transactions
– Include multiple databases updates within a single transaction
– Access multiple resources, such as a database and the Java Messaging
Service (JMS), during a transaction
Creating DataSources

• <no-tx-datasource> - a DataSource that does not take


part in JTA transactions using a java.sql.Driver
• <local-tx-datasource> - a DataSource that does not
support two phase commit using a java.sql.Driver
• <xa-tx-datasource> - a DataSource that does support
two phase commit using a javax.sql.XADataSource
Creating a DataSource
Creating an XA DataSource…
Section Review

• In this section we discussed:


– What a Data Source is and how it works
– What transactional Data Source is
– How to use the Administration Console to create a Data
Source or transactional Data Source
– How to setup JDBC Logging
Module Review

• In this module we discussed:


– The high level architecture of JDBC
– What connection pools are and how they work
– What Data Sources and Transactional Data Sources are and
how they work
– Using the Administration Console to manage JDBC
resources

You might also like