Download Sun GlassFish Enterprise Server v3 Prelude Developer`s Guide

Transcript
Creating Web Applications That Use the JDBC API
Testing a JDBC Connection Pool
You can test a JDBC connection pool for usability in one of these ways:
■
In the Administration Console, open the Resources component select Connection Pools,
and select the connection pool you want to test. Then select the Ping button in the top right
corner of the page. For details, click the Help button in the Administration Console.
■
Use the asadmin ping-connection-pool command. For details, see the Sun GlassFish
Enterprise Server v3 Prelude Reference Manual.
Both these commands fail and display an error message unless they successfully connect to the
connection pool.
Creating a JDBC Resource
A JDBC resource, also called a data source, lets you make connections to a database using
getConnection(). Create a JDBC resource in one of these ways:
■
In the Administration Console, open the Resources component and select JDBC Resources.
For details, click the Help button in the Administration Console.
■
Use the asadmin create-jdbc-resource command. For details, see the Sun GlassFish
Enterprise Server v3 Prelude Reference Manual.
Creating Web Applications That Use the JDBC API
A web application that uses the JDBC API is an application that looks up and connects to one or
more databases. This section covers these topics:
■
■
■
■
■
■
■
■
■
“Setting a Statement Timeout” on page 129
“Sharing Connections” on page 130
“Wrapping Connections” on page 130
“Obtaining a Physical Connection From a Wrapped Connection” on page 131
“Using the Connection.unwrap() Method” on page 131
“Marking Bad Connections” on page 131
“Using Non-Transactional Connections” on page 132
“Using JDBC Transaction Isolation Levels” on page 133
“Allowing Non-Component Callers” on page 134
Setting a Statement Timeout
An abnormally long running JDBC query executed by an application may leave it in a hanging
state unless a timeout is explicitly set on the statement. Setting a statement timeout guarantees
that all queries automatically time out if not completed within the specified period. When
Chapter 9 • Using the JDBC API for Database Access
129