![]() |
|
|||
|
|
|
|||
|
Both implementations use the same configuration scheme. JDBC requires that a Table 31-2.
Many application servers use a properties-style syntax to configure these properties, so it would not be unusual to enter properties as a block of text. If the application server provides a single area to enter all the properties, they might be listed like this: serverName=localhost databaseName=test user=testuser password=testpassword Or, if semicolons are used as separators instead of newlines, it could look like this: serverName=localhost;databaseName=test;user=testuser;password=testpassword
31.10.3. Applications: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JDBC | Pooling | Implementation Class |
|---|---|---|
| 2 | No | org.postgresql.jdbc2.optional.SimpleDataSource |
| 2 | Yes | org.postgresql.jdbc2.optional.PoolingDataSource |
| 3 | No | org.postgresql.jdbc3.Jdbc3SimpleDataSource |
| 3 | Yes | org.postgresql.jdbc3.Jdbc3PoolingDataSource |
All the implementations use the same configuration scheme. JDBC requires that a DataSource be configured via JavaBean properties, shown in Table 31-4, so there are get and set methods for each of these properties.
Table 31-4. DataSource Configuration Properties
| Property | Type | Description |
|---|---|---|
| serverName | String | PostgreSQL database server host name |
| databaseName | String | PostgreSQL database name |
| portNumber | int | TCP port which the PostgreSQL database server is listening on (or 0 to use the default port) |
| user | String | User used to make database connections |
| password | String | Password used to make database connections |
The pooling implementations require some additional configuration properties, which are shown in Table 31-5.
Table 31-5. Additional Pooling DataSource Configuration Properties
| Property | Type | Description |
|---|---|---|
| dataSourceName | String | Every pooling DataSource must have a unique name. |
| initialConnections | int | The number of database connections to be created when the pool is initialized. |
| maxConnections | int | The maximum number of open database connections to allow. When more connections are requested, the caller will hang until a connection is returned to the pool. |
Example 31-9 shows an example of typical application code using a pooling DataSource.
Example 31-9. DataSource Code Example
Code to initialize a pooling DataSource might look like this:
Jdbc3PoolingDataSource source = new Jdbc3PoolingDataSource(); source.setDataSourceName("A Data Source"); source.setServerName("localhost"); source.setDatabaseName("test"); source.setUser("testuser"); source.setPassword("testpassword"); source.setMaxConnections(10);Then code to use a connection from the pool might look like this. Note that it is critical that the connections are eventually closed. Else the pool will "leak" connections and will eventually lock all the clients out.
Connection con = null; try { con = source.getConnection(); // use connection } catch (SQLException e) { // log error } finally { if (con != null) { try { con.close(); } catch (SQLException e) {} } }
All the ConnectionPoolDataSource and DataSource implementations can be stored in JNDI. In the case of the nonpooling implementations, a new instance will be created every time the object is retrieved from JNDI, with the same settings as the instance that was stored. For the pooling implementations, the same instance will be retrieved as long as it is available (e.g., not a different JVM retrieving the pool from JNDI), or a new instance with the same settings created otherwise.
In the application server environment, typically the application server's DataSource instance will be stored in JNDI, instead of the PostgreSQL ConnectionPoolDataSource implementation.
In an application environment, the application may store the DataSource in JNDI so that it doesn't have to make a reference to the DataSource available to all application components that may need to use it. An example of this is shown in Example 31-10.
Example 31-10. DataSource JNDI Code Example
Application code to initialize a pooling DataSource and add it to JNDI might look like this:
Jdbc3PoolingDataSource source = new Jdbc3PoolingDataSource(); source.setDataSourceName("A Data Source"); source.setServerName("localhost"); source.setDatabaseName("test"); source.setUser("testuser"); source.setPassword("testpassword"); source.setMaxConnections(10); new InitialContext().rebind("DataSource", source);Then code to use a connection from the pool might look like this:
Connection con = null; try { DataSource source = (DataSource)new InitialContext().lookup("DataSource"); con = source.getConnection(); // use connection } catch (SQLException e) { // log error } catch (NamingException e) { // DataSource wasn't found in JNDI } finally { if (con != null) { try { con.close(); } catch (SQLException e) {} } }
| Who's Online | ||
| Guest Users: 8 |
| |
|
| |
Deliver First Class Web Sites: 101 Essential Checklists Want to learn how to make your web sites usable and accessible? Want to ensure that your sites meet current best practice, without spending hours trawling through incomprehensible specifications and recommendations from dozens of different books, research papers, and web sites? Want to make sure that the sites you build are "right the first time," requiring no costly redevelopments? |
More Sample Chapters | |
| |