Install the Driver
Glassfish ships with many JDBC drivers already included. However, I’m not going to use any of those drivers. There are two reasons I have chosen not to do this:
-
I already have a JDBC driver I use else ware that I really like
-
Your driver may not be included and I wanted to show you how to add “jars” to Glassfish so that the driver’s functionality will be available to the application server and applications that run in the application server like JSPWiki. That being said if your driver is already installed into Glassfish you might as well use the pre-installed version.
Locate the jTDS zip file you downloaded earlier. This file should be named jtds-1.2-dist.zip.
-
Decompress this file using 7-zip.
-
A new jtds-1.2-dist folder will be created
-
-
Open the jtds-1.2-dist folder
-
Locate the file named jtds-1.2.jar
-
Copy this file to the directory C:\java\glassfish-v2-b33e\lib
-
The jTDS JDBC driver is now installed! Now, we have to configure it.
Configure the Driver (Connection Pool)
Start Glassfish as described in previously.
Login to Glassfish at http://localhost:4848 as described previously
When you first log in you are presented with a “Common Tasks” screen. Click on “Create New JDBC Connection Pool.”
Enter the following values to describe our JDBC Connection Pool:
- Name: WikiPool
- Resource Type: java.sql.DataSource
- Database Vendor:
You will presented with a new screen, enter this value to tell the server how to call the jTDS driver we installed earlier:
- Datasource Classname: net.sourceforge.jtds.jdbcx.JtdsDataSource
Scroll down this screen and click on the Add Properties button. Add the appropriate name – value pairs as needed by your database as setup in an earlier step. Remember that these name -value pairs are case sensitive. For this example using MS-SQL server and the jTDS driver my Connection Pool properties are as follows:
- Name — Value
- port — 1433
- appName — GlassfishAppServer
- databaseName — dbwiki
- selectMethod — cursor
- serverName — mssql.example.com (or just use the database server ip number directly 192.168.0.6)
- user — dbuser
- password — dbpassword
Click “Finish” when you’re done entering your values.
You will be presented with a list of Connection Pools running on the server. Find WikiPool that we just setup and click on it to open up its setup screen.
On this screen click the “Ping” button. You’ll get a nice ping succeeded message with a re-assuring green check mark.
Setup a DataSource
Using the frame on the left hand side of the screen navigate to:
Resources –> JDBC –> JDBC Resources
In the right frame click the “New” button
- JNDI Name: jdbc/WikiSource
- Pool Name: WikiPool
Click “OK.”
Setup a JDBC Realm
Using the frame on the left hand side of the screen navigate to:
Configuration –> Security –> Realms
- Double click on the “Realms” folder
- Using the frame on the right hand side of the screen:
- Click the “New” button.
Enter the following values…
- Name: jdbc-realm
- Classname: com.sun.enterprise.security.auth.realm.jdbc.JDBCRealm
Click the “Add Property” and add each of the following name-value pairs
- user-table — auth
- password-column — password
- jaas-context — jdbcRealm
- group-name-column — role
- datasource-jndi — jdbc/WikiSource
- digest-algorithm — none
- group-table — authrole
- user-name-column — username
Finally, navigate to Configuration –> Security
In the right hand window …
- Check the box labeled “Default Principal to Role Mapping.”
- Click “Save”
This in a nut shell is how these parts together (sort of):
database driver connection pool data source application
\ jdbc-realm
The driver deals directly with the database. The connection pool configures and controls the driver. The data source is how the applications and, in our case, the jdbc-realm, get their hands on the pool. You will see later that we will configure our JSPWiki to make use of the jdbc-realm to control all the user login activity. Basically, the application also determines HOW to use the jdbc-realm.
Its a good idea to restart the application server at this point to make sure the addition of the jdbc-realm to the application server takes hold.
Denis, >This post has been very helpful. however, I am using MySQL and having trouble connecting to my database.>I know I need to change the DataSource Classname, but Im not sure to what. Can you help?>My email is bjaminwiz@gmail.com
LikeLike
Hi Ben:>>Give com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource or maybe com.mysql.jdbc.jdbc2.optional.MysqlDataSource a try.>>I’m not using mysql myself but I did find the above via a search on Google.>>Dennis
LikeLike
Thanks!>Actually, com.mysql.DataSource worked to connect, for those interested in using MySQL.
LikeLike
Really helpful post, but one mistake, the port porpertie name is “portNumber”.
LikeLike
Hi Dennis, I've a strange error with my realm configuration and I hope you can help me.
I've just configured my glassfish like you describe in this tutorial. But when compare the login window and I submit my username/password I receive this error message:
“Web login failed: Login failed: javax.security.auth.login.LoginException: Security Exception”
I don't know how it's possible. I follow every tutorial found in internet and everyone says the same procedure that I execute.
LikeLike