Hibernate + proxool
After a day of proxool configuration in hibernate, it was finally completed. Many problems were encountered. I would like to share with you here.
1. Official Website:
Http://sourceforge.net/projects/proxool/files/
2. Describe my configuration Environment
Jre1.6, tomcat6.0, hibernate3.1, proxool-0.9.0RC3
The database is mssql2005 and the JDBC driver is sqljdbc. jar.
3. Configure step Aggregation
(1) create a proxool. xml file, which is in the same directory as Hibernate. cfg. xml under the root directory SRC. The content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! --
The proxool configuration can be embedded within your own application's.
Anything outside the "proxool" tag is ignored.
-->
<Something-else-entirely>
<Proxool>
<Alias> mssqlproxool </alias>
<Driver-URL> JDBC: sqlserver: // XXX. XX: 1433; databasename = xxx </driver-URL>
<Driver-class> com. Microsoft. sqlserver. JDBC. sqlserverdriver </driver-class>
<Driver-Properties>
<Property name = "user" value = "sa"/>
<Property name = "password" value = "XXX"/>
</Driver-Properties>
<House-keeping-test-SQL> select current_date <House-keeping-sleep-time> 90000 <Simultaneous-Build-Throttle> 20 </simultaneous-Build-Throttle>
<Maximum-connection-count> 100 </maximum-connection-count>
<Minimum-connection-count> 10 </minimum-connection-count>
<Maximum-connection-lifetime> 3600000 </maximum-connection-lifetime>
</Proxool>
</Something-else-entirely>
Attribute list description:
Fatal-SQL-exception:
It is a comma-separated information segment. when an SQL exception occurs, its exception information will be compared with this information segment. if a part exists, this exception is considered a fatal error.
(Fatal SQL exception
). In this case, the database connection will be abandoned. No matter what happens, this exception will be re-thrown to the consumer. It is best for the user to configure a different exception to throw.
Fatal-SQL-exception-wrapper-class: As mentioned above, you 'd better configure a different exception to throw. With this attribute, You can encapsulate
Sqlexception to make it another exception. This exception can be inherited from sqlexception or the inherited word runtimeexception. proxool
Two implementations are provided: 'org. logicalcobwebs. proxool. fatalsqlexception 'and 'org. logicalcobwebs. proxool. fatalruntimeexception'. The latter is more suitable.
House-keeping-sleep-time: the maximum time for the house keeper to keep the thread in sleep state. house keeper is responsible for checking
Status of each connection and determine whether to destroy or create the connection.
House-keeping-test-SQL: if an idle database connection is found, house keeper will use this statement to test. This statement is best not
Often executed quickly. If not defined, the test process will be ignored.
Injectable-connection-Interface: method that allows proxool to implement the connection object to be proxy.
Injectable-statement-Interface: Allows proxool to implement the statement object method to be proxies.
Injectable-prepared-statement-Interface: allows the proxool tool to implement the preparedstatement object method to be proxy.
Injectable-callable-statement-Interface: allows the proxool tool to implement the callablestatement object method to be proxy.
JMX: If the attribute is true, a message Bean is registered to the JMS service. The message Bean object name is "proxool: TYPE = pool, name = <alias>". The default value is false.
JMX-agent-ID:
A comma-separated JMX proxy list (for example, using mbeanserverfactory. findmbeanserver (string
Agentid) the registered connection pool .) This attribute is valid only when the "JMX" attribute is set to "true. It is uncertain to use this attribute on all registered JMX servers.
JNDI-Name: name of the Data Source
Maximum-active-time: if housekeeper detects that the activity time of a thread is greater than this value, it will kill the thread.
So check the bandwidth of your server and set a suitable value. The default value is 5 minutes.
Maximum-connection-count: Maximum number of database connections.
Maximum-connection-lifetime: Maximum lifetime of a thread.
Minimum-connection-count: Minimum number of database connections
Overload-without-Refusal-lifetime: This helps us determine the status of the connection pool. If a connection is rejected
(MS), and is considered overload. The default value is 60 seconds.
Prototype-count: number of available connections in the connection pool. If the number of connections in the current connection pool is less than this value, the new connection will be established.
(Assuming the maximum number of available connections is not exceeded). For example, we have three active connections and two available connections, and our prototype-count is 4, then the number
The database connection pool will try to establish another two connections. This is different from minimum-connection-count. Minimum-connection-count
Calculate the number of active connections. Prototype-count is the number of spare connections.
Recently-started-threshold: This helps us determine the status of the connection pool. The number of connections is large or overloaded. As long as there is at least one connection
The connection pool is enabled if the connection pool starts to be in this value (in milliseconds) or there are some redundant available connections. The default value is 60 seconds.
Simultaneous-Build-throttle:
This is the maximum number of connections that we can establish at a time. That is, the new connection request, but there is no available connection. Since connections can use multiple threads, establishing connections within a limited time can lead to available connections,
However, we need to confirm in some ways that some threads do not respond to connection requests immediately. The default value is 10.
Statistics: connection pool usage statistics. Parameter "10 s, 1 m, 1D"
Statistics-log-level: type of log statistics trail. Parameter "error" or "info"
Test-before-Use: if it is true, the connection will be served before each connection is tested. If one connection fails, it will be discarded and the other connection will be processed, if all connections fail, a new connection will be established. Otherwise, an sqlexception exception is thrown.
Test-after-Use: if it is true, the connection will be served after each connection is tested, so that it will return to the connection pool. If the connection fails, it will be discarded.
Trace: if it is true, each executed SQL statement will be logged during the execution period (debug level). You can also register a connectionlistener (see proxoolfacade) to obtain this information.
(2) modify the hibernate. cfg. xml file. The content is as follows:
<? XML version = '1. 0' encoding = 'utf-8'?>
<! Doctype hibernate-configuration public
"-// Hibernate/hibernate configuration DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>
<! -- Generated by myeclipse hibernate tools. -->
<Hibernate-configuration>
<Session-factory>
<Property name = "hibernate. proxool. pool_alias"> mssqlproxool </property>
<Property name = "hibernate. proxool. xml"> proxool. xml </property>
<Property
Name = "hibernate. Connection. provider_class"> org. hibernate. Connection. proxoolconnectionprovider </property>
<Property name = "hibernate. proxool. existing_pool"> true </property>
<Property name = "dialect"> org. hibernate. dialect. sqlserverdialect </property>
<Property name = "hibernate. cache. use_query_cache"> true </property>
</Session-factory>
</Hibernate-configuration>
Note the following three points:
1. the alias must be consistent with that in proxool.
2. Make sure the path is correct.
3. Generally, only the above two points are described on the Internet,The parameter hibernate. proxool. existing_pool is very important.
Hibernate. proxool. existing_pool: this value is set to false. When Hibernate is called, it will initialize proxool and perform database connection and other operations;
(3) makeProgramYou can directly use the proxool connection pool in Web. xml.Configure the initialization servlet to automatically load the configuration file when the Web container is loaded
<Servlet>
<Servlet-Name> servletconfigurator </servlet-Name>
<Servlet-class> org. logicalcobwebs. proxool. configuration. servletconfigurator </servlet-class>
<Init-param>
<Param-Name> xmlfile </param-Name>
<Param-value>/WEB-INF/classes/proxool. xml </param-value>
</Init-param>
<Load-on-startup> 1 </load-on-startup>
</Servlet>
NOTE: If both web. xml and hibernate. cfg. XML are configured, an error occurs:
Org. logicalcobwebs. proxool. proxoolexception: parsing failed.
Because the proxool connection pool with the same name has been started, and hibernate will start the associated proxool connection pool itself when it starts running.
Therefore, the configuration of hibernate. cfg. xml should be changed:
<
Property
Name
=
"Hibernate. proxool. existing_pool"
>
True
</
Property
>
(4) This step is optional.Real-time Monitoring of connection pools in applications
<Servlet>
<Servlet-Name> adminproxool </servlet-Name>
<Servlet-class> org. logicalcobwebs. proxool. admin. servlet. adminservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> adminproxool </servlet-Name>
<URL-pattern>/admin/proxool </url-pattern>
</Servlet-mapping>
Access http: // localhost: 8080/project name/admin/proxool to view the page
4. Error parsing:
The common problem is
Org. hibernate. hibernateexception: proxool provider unable to load JAXP aggregator file: proxool. xml
This may be due to problems with the proxool package version, I have tried proxool-0.9.0RC1 proxool-0.9.0RC2 proxool-0.9.0RC3
The proxool-0.9.0 proxool-0.9.1 result is that both 0.9.0 and 0.9.1 of a higher version will have this problem, with RC1 or below, there is no.
We hope that the above explanation can be written as a Word document for download and help our friends who are preparing to use proxool.