Database connection and shutdown is very consuming system resources, in the multi-layer structure of the application environment, this resource consumption is directly reflected in the system performance. In the practical application of the project, the most common solution is to establish a database connection pool.
First, database connection pool Fundamentals
When the program starts, the system proactively establishes enough database connections and makes these connections a pool of connections. Each time the application requests a database connection, it does not have to be re-established to get the established connection directly from the link pool. When you are finished, you do not need to close it and return it directly to the database connection pool for other program calls. Through the connection pool, the program operation efficiency is greatly improved.
second, database connection pool common parameters
Number of connection pool initial connections
Connection pool maximum number of connections
Connection pool minimum number of connections
Each additional capacity of the connection pool
Iii. using the JBoss Management database connection pool
For example, configure the MySQL database connection pool in Itoo:
First, add the Mysql-driver jar package to the JBoss Lib directory, and configure the data source in the JBoss standalone.xml configuration file <datasources></datasources> node as follows:
[HTML]View Plain copy print?
- <!--jndi names of data sources--
- <datasource jta= "true" Jndi-name= "Java:jboss/datasources/cloudmysqlds" pool-name= "Cloudmysqlds" enabled= "true" Use-java-context= "true" >
- Url--> of <!--connected Database
- <connection-url>jdbc:mysql://192.168.22.246:3306/itoo_cloudroot?useunicode=true&characterencoding= Utf-8</connection-url>
- <driver>mysql</driver>
- <pool>
- <prefill>false</prefill>
- <use-strict-min>false</use-strict-min>
- <flush-strategy>FailingConnectionOnly</flush-strategy>
- </pool>
- <!--user name and password to connect to the database--
- <security>
- <user-name>root</user-name>
- <password>root</password>
- </security>
- </datasource>
<!--jndi name of data source--<datasource Jta= "true" Jndi-name= "Java:jboss/datasources/cloudmysqlds" Pool-nam E= "Cloudmysqlds" enabled= "true" use-java-context= "true" > <!--connection to the database url--> <connection-url>jdbc:mysql://192.168.22.246:3306/itoo_cloudroot?useunicode=true&characterencoding= Utf-8</connection-url> <driver>mysql</driver> <pool> <prefill>false</prefill> <use-strict-min>false</use-strict-min > <flush-strategy>FailingConnectionOnly</flush-strategy> </poo L> <!--connect to the database username and password--<security> <user-na Me>root</user-name> <password>root</password> </security > </DATASOURCE>
To create a database connection for the first time through a program:
[Java]View Plain copy print?
- Public Connection getconnection{
- Connection Conn=null;
- PreparedStatement Pstmt=null;
- try{
- Context ctx=new InitialContext ();
- Find DataSource through Jndi
- In JBoss, the actual jndi name must precede the configured jndi-name with the java:/prefix
- DataSource ds= (DataSource) ctx.lookup ("Java:jboss/datasources/pjmysqlds");
- Get database connection
- Conn=ds.getconnection ();
- }catch (Exception e) {
- E.printstacktrace ();
- }
- Return conn;
- }
Public Connection getconnection{connection Conn=null; PreparedStatement Pstmt=null; Try{context ctx=new InitialContext ();//Lookup DataSource//jboss through Jndi , the name of the actual jndi must be preceded by Jndi-name of the configured java:/ Prefix DataSource ds= (DataSource) ctx.lookup ("Java:jboss/datasources/pjmysqlds"); Get database connection conn=ds.getconnection (); } catch (Exception e) { e.printstacktrace (); } Return conn;}
Four, Bonus's Jndi
In the configuration database connection pool We see a familiar name: JNDI "Java naming directory Interface", one of the J2EE13 specifications.
Jndi mainly contains a number of standard APIs, through which the directories are accessed, and the objects referred to in the directory are obtained through the directory. is to associate the name with an object within the computer system, allowing the application to access the object by changing its name. Jndi has such a role to play.
v. Application of the JNDI project:
General steps for accessing objects through Jndi:
1. Create a Context object
2. Call the lookup method to find the object based on the Jndi name
3. Close the context object
Because the context is just an interface, it is common to create instances from its implementation class InitialContext.
As above through the JBoss configuration database connection pool, when the program establishes the database connection, first creates the context object through the InitialContext class, then calls the object's lookup method, obtains the database connection object through the Jndi name, and finally creates the connection.
In the Itoo project, the EJB remote call through the Bean.lookupremotebean method, through the URL address, remote access bean method, through code tracing is not difficult to find, Lookupremotebean also through Jndi use path to find objects, but not to join the Java :/prefix, but ejb:/.
[Java]View Plain copy print?
- /**
- * Find the remote EJB public method
- *
- * @version 1.0.0, February 9, 2015 2:55:14
- * @param EJBADDRESS:EJB address, ear package name +core Package name + implementation Name + interface name
- * For example: Itoo-basic-ear/itoo-basic-core-0.0.1-snapshot/studentbeanimpl!com.tgb.itoo.basic.service.studentbea N
- * @return
- * @throws namingexception
- */
- Public Object Lookupremotebean (String ejbaddress) throws Namingexception {
- Final Hashtable jndiproperties = new Hashtable ();
- Jndiproperties.put (Context.url_pkg_prefixes,
- "Org.jboss.ejb.client.naming");
- Final context context = new InitialContext (jndiproperties);
- Return Context.lookup ("EJB:" + ejbaddress);
- }
/** * Find a remote EJB public method * * @version 1.0.0, February 9, 2015 PM 2:55:14 * @param ejbaddress:ejb address, ear package name +core Package name + implementation name + Interface Name * for example: itoo-basic-ear/itoo-basic-core-0.0.1-snapshot/studentbeanimpl! Com.tgb.itoo.basic.service.StudentBean * @return * @throws namingexception */public Object Lookupremotebean (String ejbaddress) throws Namingexception { final Hashtable jndiproperties = new Hashtable (); Jndiproperties.put (context.url_pkg_prefixes, "org.jboss.ejb.client.naming"); Final context context = new InitialContext (jndiproperties); Return Context.lookup ("EJB:" + ejbaddress); }
With JBoss configuration database connection pool, a small piece of code not only understands the configuration of the connection pool, but also is familiar with the use of Jndi in one of the J2EE13 specifications. Everywhere is code, nowhere to program, not technical less, code boring, less is to find the eyes and delve into the determination.
Project experience--jboss Configuring the database connection pool