栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Hibernate-Tomcat-MySQL问题

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Hibernate-Tomcat-MySQL问题

Tomcat具有开箱即用的数据库连接池。要配置MySQL,例如:

<Context>    <!-- maxActive: Maximum number of database connections in pool. Make sure you         configure your mysqld max_connections large enough to handle         all of your db connections. Set to -1 for no limit.         -->    <!-- maxIdle: Maximum number of idle database connections to retain in pool.         Set to -1 for no limit.  See also the DBCP documentation on this         and the minEvictableIdleTimeMillis configuration parameter.         -->    <!-- maxWait: Maximum time to wait for a database connection to become available         in ms, in this example 10 seconds. An Exception is thrown if         this timeout is exceeded.  Set to -1 to wait indefinitely.         -->    <!-- username and password: MySQL username and password for database connections  -->    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.         -->    <!-- url: The JDBC connection url for connecting to your MySQL database.         -->  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"    maxActive="100" maxIdle="30" maxWait="10000"    username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"    url="jdbc:mysql://localhost:3306/javatest"/></Context>

然后,您需要按以下方式配置连接:

    public static void main(String[] args) throws Exception {        PoolProperties p = new PoolProperties();        p.setUrl("jdbc:mysql://localhost:3306/mysql");        p.setDriverClassName("com.mysql.jdbc.Driver");        p.setUsername("root");        p.setPassword("password");        p.setJmxEnabled(true);        p.setTestWhileIdle(false);        p.setTestonBorrow(true);        p.setValidationQuery("SELECT 1");        p.setTestonReturn(false);        p.setValidationInterval(30000);        p.setTimeBetweenEvictionRunsMillis(30000);        p.setMaxActive(100);        p.setInitialSize(10);        p.setMaxWait(10000);        p.setRemoveAbandonedTimeout(60);        p.setMinEvictableIdleTimeMillis(30000);        p.setMinIdle(10);        p.setLogAbandoned(true);        p.setRemoveAbandoned(true); p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");        DataSource datasource = new DataSource();        datasource.setPoolProperties(p);        Connection con = null;        con = datasource.getConnection();

现在,按照通常的方式处理JDBC代码。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/452260.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号