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
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;
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 ~ ~