To configure the data source connection pool in GlassFish

Source: Internet
Author: User
Tags auth connection pooling glassfish
Preface

How to configure a data source in your own server (GlassFish and JBoss). And how do we test in our program? Let's take a look at GlassFish, for example, to see how to configure the data source. Narrative

When we start to finish GlassFish, we can open the GlassFish console for a simple property configuration. After we start GlassFish, we open the GlassFish console (http://localhost:4848) and log in, and the default username and password are Amin and adminadmin. The console page is as follows:

Let's take a look at how to configure a database connection pool Create a new JDBC Connection pool

For specific configuration Please refer to this blog: GlassFish database connection pool configuration steps to create a new JDBC resource

Specific configuration content please refer to this blog: The latest glassfish configuration MySQL

Code test

In the application, we can use connection pooling in the following ways:

public void Loginbean () {  

         try {context  

             CTX = new InitialContext ();  

             DataSource ds = (DataSource) ctx.lookup ("Jdbc/xgtimes");  

             Connection conn = Ds.getconnection ();  

          } catch (Namingexception ex) {  

             ex.printstacktrace ();  

         }  

}  

Java EE 5 requires that resources be declared in Web.xml, and for a data source, this can be configured in Web.xml:

<resource-ref>  

   <res-ref-name>jdbc/mydb</res-ref-name>  

   <res-type> javax.sql.datasource</res-type>  

   <res-auth>Container</res-auth>  

</resource-ref >  

Note: Jdbc/xgtimes is used for the name of the data source, there are two ways of querying, we use Ctx.lookup, the other is the simplest way is to use resource injection, you declare a domain in the managed bean, and then use the annotation as follows to mark it:

@Resource (name= "Jdbc/mydb")  

private DataSource source;  
Met the Pit

1. Configure the JDBC connection pool before configuring the JDBC resource because the JDBC resource needs to refer to the JDBC Connection pool.
2. If you need to create your own database, you need to add this attribute at the data resource configuration: Connectionattributes create=true, as shown below:
Summary

The configuration process is simple, but we in this configuration process, must be careful, serious, and must stand on the shoulders of giants, to avoid stepping on some unnecessary pits.
Thank you for your reading ~ ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.