2007年1月21日星期日

MySQL and WSAD 5

I had to figure this out yesterday and documented it in a blog entry at http://www.mischiefbox.com/blog/index.php?p=70 (where I also have some JSP scriptlet code for testing the data source).

The steps I followed are below:

# Create the Test Environment server.
From the Server perspective, create a new Test Environment server. I called mine TestEnv.

# Open the Server preferences.
These are hiding. Again, from the Server perspective, in the Servers folder, there is a TestEnv.wsi server configuration file. Double-click that to bring up the Server Preferences property page.

# Add the DataSource.
On the Data Sources tab (look on the bottom of the preferences page), you can add the MySQL data source. Add a new Server JDBC Provider by pressing the Add button. I called mine MySQL, with a description giving the MySQL version, and the implementation class com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource. If you configure the data source using the traditional JDBC Driver class, you will get a one-phase commit error message.

# Add the Data Source.
With the MySQL database selected, add a new Data Source. The name should be descriptive. The JNDI name should be something similar to jdbc/dsn, like any other DataSource JNDI name. The Data source helper class name is com.ibm.websphere.rsadapter.GenericDataStoreHelper. I left the rest of the values at their default settings. Press Finish when you’re done.

# Add the JDBC URL resource property.
With the new data source highlighted, add a single resource property. The name should be url, the type java.lang.String, and the value jdbc:mysql://localhost/dbi?user=user&password=password&autoReload=true. Alternatively, you could add the user, password, and other settings as appropriate to the driver (as documented) as new resource properties, but I chose not to do so.

# Add the JAAS authentication setting.
Go to the Security tab. Add a new JAAS Authentication Entry. For an alias, I chose the JNDI name. The user ID was the same as in the JDBC URL, as was the password. For a description, I chose the prosaic, “To make the database work.” You’re done working with the server configuration, so save it and close the property editor.

# Add the external resource to the web application.

1. Switch to the Web perspective.
2. Find your web.xml file (by default, under WebContent/WEB-INF) and double click to bring up the property page.
3. Select the References tab (again, at the bottom), and then the Resource Environment sub-tab (at the top).
4. Press the Add button to add a new resource.
5. Click on the new resource name to edit it, and change this to the JNDI name you assigned to the data source above.
6. Change the type to javax.sql.DataSource (press Browse… and type this in).
7. Set the WebSphere Bindings JNDI Name to the JNDI name assigned to the data source.
8. Save and close the web.xml property editor.

# (Re)start the test environment.
In the Server perspective, right click on the server name (in the lower left corner) and select Restart on the context menu. This will force the server data source changes to be loaded.

# (Re)deploy your web application.
Deploy your web application (right click on the EAR and select “Run on Server…"). If you have a good test, you should be able to verify that your data source is working.