Struts HOW-TO series of database access

Source: Internet
Author: User
Tags define config connection pooling contains finally block implement include postgresql
Access to | data | database [access to a database]

In the design of a struts application system, it is best to define an action class as a small adapter in the middle of the web/presentation layer (presentation layer) and your business logic class (the layer that contains all the data access operations) (thin Adapter).

So, you can first define some business APIs (business APIs), which are simple Java classes. You can pass some arguments to these objects and return a collection of Java beans or Java beans from these objects. The action class is responsible for calling these objects and passing the values they return to the web/presentation layer.

Typically, you can create an action class for each business method/business class API that you need to invoke. Ideally, all of the database access code is encapsulated into these business API classes, so struts doesn't know what persistence layer you're using (persistent layer) (even if you're using a persistence layer). It only needs to pass a primary key or a query parameter, and then process the returned result bean or the bean collection. This allows you to reuse these business API classes in other applications, and you can test the business APIs that are independent of struts or HTTP environments.

The easiest way to start is to design a 1:1 program that defines an action class for each of your business API portals (entry-point). When you have a lot of experience, you can also use Dispatchaction to combine these action classes. You can even define a simple "frame" Action to invoke all of these business classes. You can find the processaction of the scaffold design in the contrib directory, which is a complete implementation of the "framework" action. With this scenario you can use fewer action classes, but you must have a deeper understanding of the underlying implementations of the struts and MVC frameworks. Don't be afraid to define too many action,struts configurations at the beginning to give you the freedom to refactor your design later, because you can flexibly change your action class without impacting the application.

Ideally, the business logic layer (business logic layer) should encapsulate all data access details, including the acquisition of database connections. However, some applications are designed to require callers to obtain a database connection from a DataSource object. When this happens, the Struts DataSource Manager allows you to configure these datasource resources when you need them.

The Struts DataSource Manager is defined in the Struts configuration file (Struts-config.xml). This manager can be used to distribute and configure any database connection pools (connection pool) that implement the Javax.sql.DataSource interface. If your DBMS or container has built-in connection pools that meet these requirements, you can choose it first.


[Jakarta Public connection pooling implementation-Basicdatasource]

If you have a local (native) implementation that does not have a connection pool, you can use the public connection pool provided by Jakarta to implement [Org.apache.commons.dbcp.BasicDataSource], which can "collaborate" with the DataSource Manager Very well. In addition, struts also contains a Genericdatasource class in its Util package, which is also a connection pooling implementation. But this is a very simple implementation and is deprecated because it may be replaced by basicdatasource or other data source implementations in later versions of struts.

The following is a data source configuration in a Struts-config.xml configuration file (implemented using the Genericdatasource data source), and you can change the settings to suit your own system.


<!--configuration for Genericdatasource wrapper-->
<data-sources>
<data-source>
<set-property
Property= "Autocommit"
Value= "false"/>
<set-property
property= "description"
Value= "Example Data Source Configuration"/>
<set-property
Property= "Driverclass"
Value= "Org.postgresql.Driver"/>
<set-property
Property= "Maxcount"
Value= "4"/>
<set-property
Property= "Mincount"
Value= "2"/>
<set-property
property= "Password"
Value= "MyPassword"/>
<set-property
property= "url"
Value= "Jdbc:postgresql://localhost/mydatabase"/>
<set-property
property= "User"
Value= "MyUserName"/>
</data-source>
</data-sources>

The configuration scenario implemented using the Basicdatasource data source is as follows:

<data-sources>
<!--configuration for Commons Basicdatasource-->
<data-source type= "Org.apache.commons.dbcp.BasicDataSource" >
<set-property
Property= "Driverclassname"
Value= "Org.postgresql.Driver"/>
<set-property
property= "url"
Value= "Jdbc:postgresql://localhost/mydatabase"/>
<set-property
Property= "username"
Value= "Me"/>
<set-property
property= "Password"
value= "Test"/>
<set-property
Property= "Maxactive"
Value= "Ten"/>
<set-property
Property= "Maxwait"
Value= "5000"/>
<set-property
Property= "Defaultautocommit"
Value= "false"/>
<set-property
Property= "Defaultreadonly"
Value= "false"/>
<set-property
Property= "Validationquery"
Value= "SELECT COUNT (*) from market"/>
</data-source>
</data-sources>

Note that you can define more than one data source in your application system, and you can define multiple data sources as needed and give them a logical name (logical name). Doing so will provide better security and scalability for your application (scalability), and you can also define a data source dedicated to testing.

Once you've configured the DataSource, you'll be able to use these data sources in your application system. The following code demonstrates how to generate a database connection through these data sources in the Execute method of the action class.

Public Actionforward Execute (
Actionmapping Mapping,
Actionform form,
HttpServletRequest request,
HttpServletResponse response)
Throws Exception
{
DataSource DataSource;
Connection CNN;

Try
{
DataSource = Getdatasource (request);
CNN = Datasource.getconnection ();
The data connection has been established, and you can do what you want to do.
}
catch (SQLException e)
{
Getservlet (). log ("Process database Connection", e);
}
Finally
{
Include the code in the finally block
To ensure that the connection is eventually closed.
Try
{
Cnn.close ();
}
catch (SQLException e)
{
Getservlet (). log ("Close database Connection", e);
}
}
}

Note: If you use a public basicdatasource, the query that you provide to the Pingquery attribute (if you set it) must be able to return at least one row of records.

Example: SELECT COUNT (*) from validtable

You can replace validtable with any valid table contained in your database.

[Using multiple data sources]

If you need to use more than one data source in a module, you can include a key attribute in the <data-source> element of the configuration file.

<data-source>
<data-source key= "A" type= "Org.apache.commons.dbcp.BasicDataSource" >
... Property configuration slightly, ditto ...
</data-source>
<data-source key= "B" type= "Org.apache.commons.dbcp.BasicDataSource" >
... Property configuration slightly, ditto ...
</data-source>
</data-source>

In your code, you can get a different data source from the key. The code is as follows:

...
Try
{
Datasourcea = Getdatasource (Request, "A");
DATASOURCEB = Getdatasource (Request, "B");
...

You can set up multiple data sources for each module as needed. However, the key attribute for each data source in the same module must be unique, because the Struts module system manages the namespace with each module as the unit.

[More content please refer to:]

Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24621.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24709.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24626.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24331.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24102.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23501.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23455.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23375.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23321.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23098.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg22713.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg21974.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg21026.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg19338.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg18323.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg14975.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg14914.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg14435.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg01562.html

Conversion/data Transfer (Transformation/data Transfer)
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg24480.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg23623.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg10195.html
Http://www.mail-archive.com/struts-user@jakarta.apache.org/msg10205.html

[Provide dynamic result set]

Most database query results will correspond to your already used actionforms one by one, and you will only need to submit your query result set to the corresponding actionform. Sometimes, however, some fields in the result set (ResultSet) are not actionform properties, or worse.

Luckily, struts's custom tag set doesn't care about the type of bean you pass to them. You can even output the result set directly. But as the result of the rally has been maintained with the database connection, and because they have all the data directly to the JSP, making the page messy. So what do we do?

Starting with Struts 1.1, you can use the newly added resultsetdynaclass to convert your result set to a Dynabeans ArrayList. The Struts custom tab set can use Dynabean properties like the general JavaBean. (For more detailed information on the Dynaactionform class, refer to the Struts User manual).

The Resultsetdynaclass class is already contained in the Beanutils.jar package, so all you have to do now is just take out the tool and implement your own delivery plan ...


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.