Configuring datasources in GlassFish

In Chapter 4, Creating JEE Database Applications, we created the JDBC datasource locally in the application. In this chapter, we will create a JDBC datasource in GlassFish. GlassFish Server is not packaged with the JDBC driver for MySQL. So, we need to place the .jar file for MySQLDriver in the path where GlassFish can find it. You can place such external libraries in the lib/ext folder of the GlassFish domain in which you want to deploy your application. For this example, we will copy the JAR in <glassfish_home>/glassfish/domains/domain1/lib/ext.

If you do not have the MySQL JDBC driver, you can download it from http://dev.mysql.com/downloads/connector/j/:

  1. Open the GlassFish admin console, either by right-clicking on the server in the Servers view and selecting GlassFish | View Admin Console (this opens the admin console inside Eclipse) or browsing to http://localhost:4848 (4848 is the default port to which the GlassFish admin console application listens). In the admin console, select Resources | JDBC | JDBC Connection Pools. Click the New button on the JDBC Connection Pool page:
Figure 7.9: Create JDBC Connection Pool in GlassFish
  1. Set Pool Name as MySQLconnectionPool and select javax.sql.DataSource as Resource Type. Select MySql from the Database Driver Vendor list and click Next. In the next page, select the correct Datasource Classname (com.mysql.jdbc.jdbc2.optional.MysqlDatasource):
Figure 7.10: JDBC Connection Pool settings in GlassFish
  1. We need to set the hostname, port, username, and password of MySQL. In the admin page, scroll down to the Additional Properties section and set the following properties:

Properties

Values

Port/PortNumber

3306

DatabaseName

<schemaname_of_coursemanagement>, for example,

course_management. See Chapter 4, Creating JEE Database Applications, for details on creating the MySQL schema for the Course Management database.

Password

MySQL database password.

URL/Url

jdbc:mysql://:3306/<database_name> , for example,

jdbc:mysql://:3306/course_management

ServerName

localhost

User

MySQL username

  1. Click Finish. The new connection pool is added to the list in the left pane. Click on the newly added connection pool. In the General tab, click on the Ping button and make sure that the ping is successful:
Figure 7.11: Test JDBC Connection Pool in GlassFish
  1. Next, we need to create a JNDI resource for this connection pool so that it can be accessed from the client application. Select the Resources | JDBC | JDBC Resources node in the left pane. Click the New button to create a new JDBC resource:
Figure 7.12: Test JDBC Connection Pool in GlassFish
  1. Set JNDI Name as jdbc/CourseManagement. From the Pool Name drop-down list, select the connection pool that we created for MySQL, MySQLconnectionPool. Click Save.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.178.181