configuring db pool in glassfish
TRANSCRIPT
-
8/7/2019 Configuring DB pool in GlassFish
1/25
1
Configuring a MySQL Data Source in Glassfish Application Server
1.
Start your Glassfish Application server.
2. Choose File > New Project (Ctrl-Shift-N; -Shift-N on Mac) from the mainmenu. Select the Java Web category, then select Web Application.
-
8/7/2019 Configuring DB pool in GlassFish
2/25
2
3. The New Project wizard allows you to create an empty web application in astandard IDE project. The standard project uses an IDE-generated Ant build
script to compile, deploy, and run the application.
4. Click Next.
5. In Project Name, enter the project name SampleDB. Also, specify thelocation for the project on your computer. (By default, the IDE places projects in
a NetBeansProjects folder located in your home directory.)
6. Click Next.
7. In step 3, Server and Settings, specify the GlassFish server as server which willbe used to run the application.
8. In the Java EE Version field, select Java EE 5.
-
8/7/2019 Configuring DB pool in GlassFish
3/25
3
9. Click Finish. The IDE creates a project template for the entire application, andopens an empty JSP page (index.jsp) in the editor. index.jsp serves as the
welcome page for the application.
10.Go to Services tab Expand the Databases option and right click the mouse atMySQL server at localhost and click connect.
11.The MySQL properties dialog box will be displayed with defaultproperties.Check the server,user name and password properties then click OK
button.
-
8/7/2019 Configuring DB pool in GlassFish
4/25
4
Preparing Communication between the Application and Database
The most efficient way to implement communication between the server and database isto set up a database connection pool.
Creating a new connection for each client request can be very time-consuming, especially
for applications that continuously receive a large number of requests. To remedy this,
numerous connections are created and maintained in a connection pool. Any incoming
requests that require access to the application's data layer use an already-created
connection from the pool. Likewise, when a request is completed, the connection is not
closed down, but returned to the pool.
After preparing the data source and connection pool for the server, you then need to
instruct the application to use the data source. This is typically done by creating an entry
in the application's web.xml deployment descriptor. Finally, you need to ensure that the
database driver (MySQL Connector/J JDBC Driver) is accessible to the server.
a. Setting up a JDBC data source and connection poolb. Referencing the data source from the application
Setting up a JDBC data source and connection pool
The GlassFish Server Open Source Edition contains Database Connection Pooling
(DBCP) libraries that provide connection pooling functionality in a way that is
transparent to you as a developer. To take advantage of this, you need to configure a
JDBC (Java Database Connectivity) data source for the server which your application
can use for connection pooling.
You could configure the data source directly within the GlassFish server Admin Console,
or, using File Wizard, you can declare the resources that your application needs in a sun-
web.xml file. When the application is deployed, the server reads in the resource
declarations, and creates the necessary resources.
-
8/7/2019 Configuring DB pool in GlassFish
5/25
5
1. Login to GlassFish admin console. The default url for GlassFish admin consolewill be http://localhost:4848/login.jsf.
2. From Common Task menu expand Resources menu by clicking on Resourcesmenu.
3. Expand JDBC under resources.4. Click on Connection Pools under JDBC menu. The Connection Pools page will
be displayed.
5. Click on New.6. Enter a name for your JDBC Connection pool. Select javax.sql.DataSource as
your Resource Type.
-
8/7/2019 Configuring DB pool in GlassFish
6/25
6
7. Select MySQL as your Database vendor.
8. Click Next.
-
8/7/2019 Configuring DB pool in GlassFish
7/25
7
9. Fill the details according to your need. Minimum you need to fill the followingfields.
i. Userii. Password
iii. URL (The format will be jdbc:mysql://localhost:3306/test. Where testis your database name.)
10.Click Finish. You will be taken back to the Connection Pools page.
-
8/7/2019 Configuring DB pool in GlassFish
8/25
8
11.Click on the Connection Pool you created. You will be taken to Edit ConnectionPool page.
12.Click on Ping. If your connection pool is setup correctly you will get a PingSucceeded message.
-
8/7/2019 Configuring DB pool in GlassFish
9/25
9
13.Now click on JDBC Resources under JDBC menu.
14.Click New.15.Enter a JNDI Name for your data source. Select the pool you created by
following the above steps as your Pool Name.
-
8/7/2019 Configuring DB pool in GlassFish
10/25
10
16.Click OK. You are done.
17.You can view the created Connection Pool and the JDBC Resources in Servicestab.
-
8/7/2019 Configuring DB pool in GlassFish
11/25
11
Referencing the data source from the application
You need to reference the JDBC resource you just configured from the web application.
To do so, you can create an entry in the application's web.xml deployment descriptor.
Deployment descriptors are XML-based text files that contain information describing
how an application is to be deployed to a specific environment. For example, they are
normally used to specify application context parameters and behavioral patterns, security
settings, as well as mappings for servlets, filters and listeners.
Do the following to reference the data source in the application's deployment descriptor.
1. In the Projects window, expand the Configuration Files folder and double-clickweb.xml. A graphical interface for the file displays in the IDE's main window.
2. Click the References tab located along the top of the editor. Expand the ResourceReferences heading, then click Add. The Add Resource Reference dialog opens.
-
8/7/2019 Configuring DB pool in GlassFish
12/25
12
3. For Resource Name, enter the resource name that you gave when configuring thedata source for the server above (jndi name). The Description field is optional,
but you can enter a human-readable description of the resource.
4. Click OK. The new resource is added under the Resource References heading.5. To verify that the resource is now added to the web.xml file, click the XML tab
located along the top of the editor. Notice that the following tags
are now included:
-
8/7/2019 Configuring DB pool in GlassFish
13/25
13
6. You need to refer the resource reference in sun-web.xml.The added jndi name inweb.xml will be automatically included in sun-web.xml Resource Reference
section.
7. Expand the resource reference name.
8. Enter the JNDI name, Principal name(user name in MySQL) and PrinipalPassword(Password in MySQL).
-
8/7/2019 Configuring DB pool in GlassFish
14/25
14
9. Click Save.10.To obtain a connection using the above DataSource, use the following code.
InitialContext ctx =newInitialContext();
DataSource ds =(DataSource) ctx.lookup("java:comp/env/jndiname");
Connection con = ds.getConnection();
Jndiname : The name that you entered in sun-web.xml.
Note:
Connection pool and JNDI name can also be created using File Wizard. The
steps are given below.
-
8/7/2019 Configuring DB pool in GlassFish
15/25
15
Setting up a JDBC data source and connection pool using Resource
Wizard
The following steps demonstrate how to declare a connection pool, and a data source that
relies on the connection pool. The NetBeans JDBC Resource wizard allows you to
perform both actions.
1. Open the New File wizard by pressing the New File ( ) button in the IDE'smain toolbar. Select the GlassFish server category, then select JDBC Resource
and click Next.
-
8/7/2019 Configuring DB pool in GlassFish
16/25
16
-
8/7/2019 Configuring DB pool in GlassFish
17/25
17
2. In step 2, General Attributes, choose the Create New JDBC Connection Pooloption, then in the JNDI Name text field, type in jdbc/samplejndi.
3. Optionally, add a description for the data source.4. Click Next, then click Next again to skip step 3, Additional Properties.
-
8/7/2019 Configuring DB pool in GlassFish
18/25
18
5. In Step 4, type in samplejndiPool for JDBC Connection Pool Name. Make surethe Extract from Existing Connection option is selected, and choose
jdbc:mysql://localhost:3306/MyNewDatabase from the drop-down list.
MyNewDatabase = The Schema in mysql at your localhost.
-
8/7/2019 Configuring DB pool in GlassFish
19/25
19
Note: The wizard detects any database connections that have been set up in the IDE.
Therefore, you need to have already created a connection to the MyNewDatabase
database at this point. You can verify what connections have been created by opening
the Services window (Ctrl-5; -5 on Mac) and looking for connection nodes ( ) under
the Databases category.
6. Click Next. In Step 5, note that the IDE extracts information from the databaseconnection you specified in the previous step, and sets name-value properties for
the new connection pool.
-
8/7/2019 Configuring DB pool in GlassFish
20/25
20
-
8/7/2019 Configuring DB pool in GlassFish
21/25
21
7. Click Finish. The wizard generates a sun-resources.xml file that containsentries for the data source and connection pool you specified.
-
8/7/2019 Configuring DB pool in GlassFish
22/25
22
Referencing the data source from the application
You need to reference the JDBC resource you just configured from the web application.
To do so, you can create an entry in the application's web.xml deployment descriptor.
Deployment descriptors are XML-based text files that contain information describing
how an application is to be deployed to a specific environment. For example, they are
normally used to specify application context parameters and behavioral patterns, security
settings, as well as mappings for servlets, filters and listeners.
Do the following to reference the data source in the application's deployment descriptor.
11.In the Projects window, expand the Configuration Files folder and double-clickweb.xml. A graphical interface for the file displays in the IDE's main window.
12.Click the References tab located along the top of the editor. Expand the ResourceReferences heading, then click Add. The Add Resource Reference dialog opens.
-
8/7/2019 Configuring DB pool in GlassFish
23/25
23
13.For Resource Name, enter the resource name that you gave when configuring thedata source for the server above (jndi name). The Description field is optional,
but you can enter a human-readable description of the resource.
14.Click OK. The new resource is added under the Resource References heading.15.To verify that the resource is now added to the web.xml file, click the XML tab
located along the top of the editor. Notice that the following tags
are now included:
-
8/7/2019 Configuring DB pool in GlassFish
24/25
24
16.You need to refer the resource reference in sun-web.xml.The added jndi name inweb.xml will be automatically included in sun-web.xml Resource Reference
section.
17.Expand the resource reference name.
18.Enter the JNDI name, Principal name(user name in MySQL) and PrinipalPassword(Password in MySQL).
-
8/7/2019 Configuring DB pool in GlassFish
25/25
25
19.Click Save.20.To obtain a connection using the above DataSource, use the following code.InitialContext ctx =newInitialContext();
DataSource ds =(DataSource)
ctx.lookup("java:comp/env/jndiname");
Connection con = ds.getConnection();