Configuring a MySQL JDBC Connection Pool and JDBC Resource for GlassFish using NetBeans
This tutorial assumes that you have installed the Java EE version of NetBeans 8.02. If you are using Windows then it is further assumed that you have replaced the default GlassFish server instance in NetBeans with a new instance with its own folder for the domain. See my previous article “Creating a New Instance of GlassFish in NetBeans IDE” On an Apple Mac and likely on Linux it is not necessary to do this.
The other day I presented to my students the steps necessary to make GlassFish responsible for JDBC connections. As I went through the steps I realized that I needed to record the steps for my students to reference. Here then are these steps.
Step 1: Add the MySQL driver to the GlassFish Domain
Before we even start NetBeans we must do a bit of preliminary work. GlassFish does not include the MySQL driver or any other driver in its distribution. One exception is that it does include a complete implementation of the Derby database and Derby is the default data source for GlassFish.
Go to the MySql Connector/J download site at http://dev.mysql.com/downloads/connector/j/ and download the latest version. I recommend downloading the Platform Independent version. If your OS is Windows download the ZIP archive otherwise download the TAR archive. You are looking for the driver file named mysql-connector-java-5.1.34-bin.jar in the archive.
The driver file must now be copied to the lib folder in the directory where you placed your domain. On my system the folder is located at C:/Users/Ken/personal_domain/lib. If GlassFish is already running then you will have to restart it so that it picks up the new library.
Step 2: Create a Database Connection in NetBeans
One feature I have always liked in NetBeans is that it has an interface for working with databases. All that is required is that you create a connection to the database. It also has additional features for managing a MySQL server but we won’t need those. If you have not already started your MySQL DBMS then do that now. I also assume that the database you wish to connect to already exists.
Go to the Services tab and right mouse click on New Connection .
In the next dialog you must choose the database driver you wish to use. It defaults to Java DB (Embedded) . Pull down the combo box labeled Driver: and select MySQL (Connector/J driver) .
Click on Next and you will now see the Customize Connection dialog. Here you can enter the details of the connection. On my system the server is localhost and the database name is Aquarium . Here is what my dialog looks like.
Notice the Test Connection button. I have clicked on mine and so I have the message Connection Succeeded . Click on Next .
There is nothing to do on this dialog so click on Next .
On this last dialog you have the option of assigning a name to the connection. By default it uses the URL but I prefer a more meaningful name. I have used AquariumMySQL .
Click on Finish and the connection will appear under Databases.
If the icon next to AquariumMySQL has what looks like a crack in it similar to the jdbc:derby connection then this means that a connection to the database could not be made. Verify that the database is running and is accessible. If it is then delete the connection and start over.
Step 3: Create a Web Application Project in NetBeans
If you have not already done so create a New Project in NetBeans. My preference is to create a New Project in the Maven category of a Web Application project. You could also create a New Project of the Java Web category of a Web Application project.
Click on Next . In this next dialog you can give the project a name and a location in your file system. The Artifact Id, Group Id and Version are used by Maven.
The final dialog lets you select the application server that your application will use and the version of Java EE that your code must be compliant with.
Here is my project ready for the next step.
Step 4: Create the GlassFish JDBC Resource
For GlassFish to manage your database connection you need to set up two resources, a JDBC Connection Pool and a JDBC Resource. You can create both in one step by creating a GlassFish JDBC Resource because you can create the Connection Pool as part of the operation.
Right mouse click on the project name and select New and then Other …
Scroll down the Categories list and select GlassFish . In the File Types list select JDBC Resource .
Click on Next . The next dialog is the General Attributes . Click on the radio button for Create New JDBC Connection Pool . In the text field JNDI Name enter a name that is unique for the project. JNDI names for connection resources always begin with jdbc/ followed by a name that starts with a lower case letter. I have used jdbc/myAquarium .
Click on Next . There is nothing for us to enter on the Properties dialog.
Click on Next . On the Choose Database Connection dialog we will give our connection pool a name and select the database connection we created in Step 2. Notice that in the list of available connections you are shown the connection URL and not the name you assigned to it.
Click on Next . On the Add Connection Pool Properties dialog you will see the connection URL and the user name and password. We do need to make one change. The resource type shows javax.sql.DataSource and we must change it to javax.sql.ConnectionPoolDataSource .
Click on Next. There is nothing we need to change on Add Connection Pool Optional Properties so click on Finish.
A new folder has appeared in the Projects view named Other Sources . It contains a sub folder named setup . In this folder is the file glassfish-resources.xml .
The glassfish-resources.xml file will contain the following. I have reformatted the file for easier viewing.
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"><resources><jdbc-resource enabled="true" jndi-name="jdbc/myAquarium" object-type="user" pool-name="aquariumPool"><description/></jdbc-resource><jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="aquariumPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.ConnectionPoolDataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false"><property name="URL" value="jdbc:mysql://localhost:3306/Aquarium?zeroDateTimeBehavior=convertToNull"/><property name="User" value="fish"/><property name="Password" value="fish"/></jdbc-connection-pool></resources>
Step 4: Configure GlassFish with glassfish-resources.xml
The glassfish-resources.xml file when included in the application’s WAR file should configure the resource and pool for the application. We can also use this file to configure GlassFish directly and therefore verify that it is the correct configuration.
Go to the Services tab and select Servers and then right mouse click on GlassFish . If GlassFish is not running then click on Start. You can tell if GlassFish is running based on whether Start is black (not started) or grey (started).
With the server started click on View Domain Admin Console .
Your web browser will now open and show you the GlassFish console. If you assigned a user name and password to the server you will have to enter this information before you see the console.
In the Common Tasks tree select Resources . You should now see in the panel adjacent to the tree the following.
Click on Add Resources . You should now see:
In the Location click on Choose File and locate your glassfish-resources.xml file. Mine is found at D:/NetBeansProjects/GlassFishTutorial/src/main/setup. You should now see:
Click on OK . If everything has gone well you should see:
The final task in this step is to test if the connection works. In the Common Tasks tree select Resources , JDBC , JDBC Connection Pools and aquariumPool .
Click on Ping . You should see:
If the Ping was not successful review the steps.
The resources are now visible in NetBeans.
You are ready to code!
Axure汉化版已经发布，版本号Axure 220.127.116.119，下面是截图效果 Axure汉化版文件下载地址：Axure汉化补丁 Axure RP pro 18.104.22.1689 下载地址注册用户名：Axure 序列...
BI中文站 6月7日报道 艾默生·斯帕茨(Emerson Spartz)今年28岁，已婚，是Spartz Inc公司的首席执行官。Spartz Inc是一个网站媒体帝国，旗下的30多家网站专门发布有趣、励志和让人感到不可思议的文章和帖子，其分享量非常...
- mysql 将字段time按天/月/年分组
- 新安装mysql 第三方工具连接不上问题
- CentOS 安装MySQL 5.1.69
- mysql出现“Incorrect key file for table”解决办法
- mysql无法启动——cannot allocate the memory for the buffer pool
- Mysql 日志删除
- mysql 修改字段类型 删除字段类型
- 修改mysql字符编码出现Job failed to start解决办法
- Why TokuDB hates Transparent HugePages