config-database.xtp
来自「RESIN 3.2 最新源码」· XTP 代码 · 共 729 行 · 第 1/2 页
XTP
729 行
<var>Driver</var> is the original JDBC interface, and is the least desirable kindof driver to use. Resin can still pool database connections using these drivers, but it will not be as efficient as the newer drivers.</p></s3></s2> <!-- choosing-driver --><s2 name="init-param" title="Set driver properties with init-param"><p><var>init-param</var> is used to set properties of the database driver that arespecific to the driver and are not generic enough for resin to provide anamed configuration tag.</p><p>For example, MySQL drivers accept the <code>useUnicode</code> parameter, iftrue the driver will use Unicode character encodings when handlingstrings.</p><example title="Example: mysql configuration"><database> <jndi-name>jdbc/mysql</jndi-name> <driver> <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type> <url>jdbc:mysql://localhost:3306/dbname</url> <user>username</user> <password>password</password> <b><init-param useUnicode="true"/></b> </driver> ...</database></example></s2> <!-- init-driver --></s1> <!-- driver-config --><s1 name="pooling" title="Pooling Configuration"><p>Pooling configuration controls the behaviour of Resin's pooling of databaseconnections. For most applications and databases the only needed change is toincrease the max-connections value to meet high demand. Other poolingparameters have defaults that are based on our years of experience with manydifferent databases in many different applications. Changes from the defaultsshould only be done in response to specific problems, and with a goodunderstanding of how pooling works.</p></s1><s1 name="reliability" title="Reliability Configuration"><s2 name="reliability-ping" title="ping"><p>Resin's database pool can test if the pooled database connectionis still alive by configuring a <var>ping</var> query. This is typically onlynecessary if the <a href="#pooling">pooling</a> parameters are changed fromtheir default values.</p><p>If the pool is configured with a long max-idle-time the database connectionmay become stale if the database is restarted, or if the database is configuredwith a shorter connection timeout value than the configuration of the Resinpool. Normally when a database connection is returned to the pool it will waitthere until the next request or the idle-time expires. If the database goesdown in the meantime or closes the connection, the connection will becomestale. The <var>ping</var> configuration can test the database connection.</p><p>When pinging, Resin's DBPool will test a table specified with the<var>ping-table</var> parameter before returning the connection to the application.If the ping fails, the connection is assumed to be no good and a differentconnection from the pool is returned. For a ping-table of BROOMS, Resin willuse the query <code>select 1 from BROOMS where 1=0</code></p><example title="Example: <ping> configuration"> <database jndi-name="..."> <driver type="..."> ... </driver> <ping>true</ping> <ping-table>BROOMS</ping-table> </database></example><p>You can test the ping using the following steps:</p><ol><li>Configure the database with ping-table and ping.</li><li>Execute some servlet that queries the database.</li><li>Restart the database server.</li><li>Execute another servlet that queries the database.</li></ol></s2> <!-- reliability-ping --><s2 name="driver_list" title="<driver> list"><p>If there is a pool of database servers available that can be used for databaseoperations, Resin can be configured with a list of <driver> tags. Resin uses a round robin algorithm to cycle through the list of drivers whenobtaining connections. If a particular <driver> fails to provide aconnection, Resin continues the attempt to obtain a connection. If all of theconfigured drivers fail to provide a connection the exception is propogated tothe caller.</p><example title="Example: A <driver> list"><database jndi-name="jdbc/hogwarts"> <driver> <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type> <url>jdbc:mysql://192.168.0.<b>110</b>:3306/hogwarts</url> ... </driver> <driver> <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type> <url>jdbc:mysql://192.168.0.<b>111</b>:3306/hogwarts</url> ... </driver> ...</database></example><s3 name="backup-driver" title="<backup-driver> list"><p>Drivers in a driver list can be marked as backups. The drivers configured with<backup-driver> are used only if all of the drivers configured with<driver> have failed.</p><p>Each time a new connection is needed Resin goes through the process of firstattempting to use one of the <driver> configured drivers to get a connection,and if that fails then the <backup-driver> are used.A new connection is needed from the driver if the pool of connections that ismaintained by Resin does not contain an idle connection. The<a href="#pooling">Pooling configuration</a> and the usage pattern of theapplication determine how often a connection is obtained from a driver. Thepooling configuration typically allows a single real connection to be reused bythe application many times.</p><p>The lifetime of a connection obtained from a <backup-driver> is determined bythe <a href="#pooling">Pooling configuration</a>, thus even if the main<driver> becomes available again a connection previously obtained from a<backup-driver> will continue to be used until it expires from the pool.</p><example title="Example: A <backup-driver> list"><database jndi-name="jdbc/hogwarts"> <driver> <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type> <url>jdbc:mysql://192.168.0.<b>110</b>:3306/hogwarts</url> ... </driver> <driver> <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type> <url>jdbc:mysql://192.168.0.<b>111</b>:3306/hogwarts</url> ... </driver> <backup-driver> <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type> <url>jdbc:mysql://192.168.0.<b>112</b>:3306/hogwarts</url> ... </backup-driver> <backup-driver> <type>com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource</type> <url>jdbc:mysql://192.168.0.<b>113</b>:3306/hogwarts</url> ... </backup-driver> ...</database></example></s3></s2> <!-- driver-list --></s1> <!-- reliability --><s1 name="usage" title="Obtaining and using a database connection"><s2 name="usage/lookup" title="Getting the DataSource"><p>The <code>DataSource</code> is a factory that is used to obtaina connection. The <code>DataSource</code> is obtained using the <a config-tag="jndi-name"/> specified when configuring the database resource. </p><p>Ideally, the JNDI lookup of <code>DataSource</code> is done only once, the<code>DataSource</code> obtained from the lookup can be stored in a member variableor other appropriate place. The stored <code>DataSource</code> can then be usedeach time a connection is needed. If it is not stored, there will be an impacton performance from having to do the lookup each time you want to get aconnection.</p><example title="Example: Obtaining a DataSource">import javax.sql.*;import javax.webbeans.*;public class .... { @Named("jdbc/test") DataSource _pool; ...}</example></s2> <!-- usage/lookup --><s2 name="usage/get-connection" title="Getting a Connection"><p>A connection is obtained from the <code>DataSource</code>. Theconnection is used as needed, and then released with a call to close() so thatResin knows it is available for a subsequent request.</p><p>It is very important that the <code>close()</code> is <i>always</i> called, evenif there as an exception. Without the <code>close()</code>, Resin's database poolcan loose connections. If you fail to close() a connection, Resin does not knowthat it is available for reuse, and cannot allocate it for another request.Eventually, Resin may run out of connections. </p><warn>Always put a <code>close()</code> in a finally block, to guarantee that it is called.</warn><p>The following example shows the use of a <code>finally</code> block that containsthe <code>close()</code>. Because the <code>close()</code> is in a finally block, itwill happen even if the code using the connection throws an exception.</p><example title="Example: Getting a connection from the DataSource">package javax.webbeans.*;package javax.sql.*;public class MyBean(){ @In DataSource _pool; public void doStuff() { Connection conn = null; try { conn = _pool.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(" ... "); ... rs.close(); stmt.close(); } catch (SQLException e) { throw new ServletException(e); } finally { try { if (conn != null) conn.close(); } catch (SQLException e) { } } }}</example></s2> <!-- usage/get-connection --><s2 name="usage/get-driver-connection" title="Getting the underlying driver connection"><p>The connection obtained by <code>pool.getConnection()</code> is an instance of <a href="javadoc|com.caucho.sql.UserConnection|"/>.</p><p><code>UserConnection</code> is a wrapper around the real driver connection, itallows Resin to intercept the close() call and manage the underlying driverconnection.</p><p>In rare circumstances it is necessary to obtain the real connection returned by the driver.Typically this is a requirement for situations where the driver provides aspecialized API that is not available with the standard JDBC API.</p><example title="Example: Getting the underlying driver connection">Connection driverConn = ((com.caucho.sql.UserConnection) connection).getConnection();// never do this: driverConn.close()</example></s2></s1> <!-- usage --><s1 title="Protecting the database password"><p>Resin provides facilities that allow you to plugin your own customcode that returns a password to Resin. However any solution is vulnerable,unless you require a person to type in a password every time Resin starts (orrestarts). Typically the security of the machine hosting Resin, and properpermissions on the readability of the resin.xml file, are sufficient toprotect your database password.</p><p>The solution shown below is not really secure because you can disassemble thePassword code to get the decryption key, but it may be marginally better thanplaintext.</p> <example title="Example: password encryption"><driver type="..."> <password resin:type="com.hogwarts.Password">mX9aN9M==</password> ...</example> <p>You will need to provide com.hogwarts.Password:</p> <example title="Example: Password class">package com.hogwarts;public class Password { private String _value; public void addText(String value) { _value = value; } public Object replaceObject() { return decrypt(_value); } private String decrypt(String encrypted) { ... custom code ... }} </example> <p>This solution is completely general, you can use resin:type anywhere in theconfiguration files where a string value is allowed.</p> <p>Resin does not provide the equivalent of com.hogwarts.Password because it's notreally secure. Providing that kind of solution would lead some to believe itwas a secure solution.</p></s1></body></document>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?