J F W

making web application programming easier is possible

HOW TO : CORE

get database connections

If you don't want to use the classes of JFW for executing a sql command or a stored procedure, you can still use the classes that distribute database connections.
First of all, you have to configure the jfw.properties. As you cans see in "HOW TO : WEBAPP > jfw.properties" you can use the JFW connection pool mechanism or a data source defined outside the JFW, for example using the application server.

To get a database connection, you have to use the class ConnectionBroker:

The db1 is the logical name of a database resource as is defined in the parameter db_resources_names of jfw.properties.

The database connection will be got from the connection pool or by the data source and will be returned to the caller. In that way you can change dynamically the types of your database resources, between connection pools and data sources, without changing the code.

For releasing the connection, you have to do this:

The connection object passed to the method release is the one previously returned by method get.

If you want to make a database transaction, you can use the class Transaction. To create a transaction for the default database resource, you can use the constructor with no parameters; if you want to use a different database resource you have to use the other constructor.

The method getConnection returns a Connection object. The returned connection is always the same.

When you have finished the transaction, you can use the method close: the boolean parameter is used to make the commit (if true) or the rollback (if false) before the connection is released.

Here is a typical use of the Transaction class: