Database Guide

From Stax

Jump to: navigation, search

Contents

Using Stax MySQL

To create a MySQL database for your application to use, just login to the Stax AppConsole and click the Create DB button. You can then choose a name for your database (it must be unique), and assign the username/password that applications must use to connect to the database.

Using the database from an application

After creating your database and selecting it in the Stax AppConsole, review the Cribsheet to see instructions for using the database from an application.

The basic steps are:

  • Declare a datasource in your application's web.xml file
  • Bind the datasource to your database in the stax-application.xml file
  • Write the code for using the database

The cribsheet provides XML and code snippets that make this process very straightforward.

Connecting to the database with a MySQL client

Management of your database is typically done using a MySQL client like the 'mysql' command line program distributed with MySQL, or the MySQL Query Browser. To connect to your Stax database with one of these tools, just enter the remote connection settings found on the Overview section of your database in the Stax AppConsole.

Note: the server info used to connect to your database can change if your database is redeployed for any reason in the Stax environment, so you will need to update your database connection if this happens. Your application JDBC URL does not use an explicit server name, so your applications will not break when databases are redeployed in the Stax service.

Avoiding Database Idle Timeouts

MySQL includes a timeout that will close connections that have been idle for long periods of time. For improved Database performance, Stax DataSources use the Apache DBCP connection pool to reuse JDBC connections after they are closed by the application. If you use a connection that has been in the pool idle for too long, your application will likely experience the following error: "The last packet successfully received from the server was XXX seconds ago". The connection pool includes a setting the will validate and throw out dead connections when calling javax.sql.DataSource.getConnection(). To use this setting, add the following XML params to your DataSource definition in stax-web.xml (or stax-application.xml)

<param name="validationQuery" value="SELECT 1" />
<param name="testOnBorrow" value="true" />

Using External Databases

To connect your application to a non-Stax database, define the database as a resource-ref in the app web.xml file,and bind it to your database as a resource in the stax-web.xml file and include the standard JDBC connection settings for connecting to the database.

WEB-INF/stax-web.xml

<resource name="jdbc/DATASOURCE_NAME" auth="Container" type="javax.sql.DataSource">
 <param name="username" value="USERNAME" />
 <param name="password" value="PASSWORD" />
 <param name="url" value="JDBC_URL" />
</resource>

WEB-INF/web.xml

<resource-ref>
 <res-ref-name>jdbc/DATASOURCE_NAME</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>
Personal tools
Navigation