【问题标题】:Setup database connection with tomcat 7使用 tomcat 7 设置数据库连接
【发布时间】:2016-03-27 15:26:22
【问题描述】:

我有一个使用 MySQL、Spring 和 tomcat 服务器的工作程序,但数据库连接在 spring-database.xml 中:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="baseController" class="com.afterguard.sailplanner.controller.BaseController">
        <property name="dao" ref="daoImpl" />
    </bean>

    <bean id="daoImpl" class="com.afterguard.sailplanner.dao.DaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/sailplanner" />
        <property name="username" value="sailplanner" />
        <property name="password" value="sailplanner2" />
    </bean>

</beans>

现在为了在服务器上部署这个程序,我需要把这个数据库连接带到服务器上,例如context.xml 和 server.xml。

如何设置我的 tomcat 7 服务器以连接到数据库并告诉我的应用程序,数据库连接已在 tomcat 中设置?

【问题讨论】:

  • 你想要什么不清楚,请说明你的要求。
  • 好的,我试图澄清这个问题。
  • 将数据库连接带到服务器是什么意思?在其上创建一个数据库连接,或者只是将 XML 文件部署到您的应用程序中?
  • 我从你的问题中了解到的你想在你的 applicationContext.xml 文件中使用 spring-database.xml 如果是,那么你应该在 applicationContext.xml 文件中编写这段代码

标签: java mysql spring tomcat


【解决方案1】:

你必须使用 JNDI 创建一个数据源:

首先,在你的 spring bean 配置(spring-database.xml)中添加:

 <beans:bean id="dbDataSource"  class="org.springframework.jndi.JndiObjectFactoryBean">
    <beans:property name="jndiName" value="java:comp/env/jdbc/myDB"/>
</beans:bean>

其次,在文件apache-tomcat/conf/server.xml中创建jndi资源:

<Resource name="jdbc/globalDB"
  global="jdbc/globalDB"
  auth="Container"
  type="javax.sql.DataSource"
  driverClassName="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/sailplanner"
  username="sailplanner"
  password="sailplanner2"/>

最后,在文件 apache-tomcat/conf/context.xml 中创建资源链接:

<ResourceLink name="jdbc/myDB"
                global="jdbc/globalDB"
                auth="Container"
                type="javax.sql.DataSource" />

【讨论】:

  • 谢谢。正是我想要的。开箱即用。
【解决方案2】:

对于 Sql Server,我进行以下配置:

  1. 您必须使用 Tomcat 来管理连接。为此,我们将向 TOMCAT_HOME/conf/context.xml 文件中添加数据源

    <Context>
    <Resource
        name="jdbc/Oltp1"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="net.sourceforge.jtds.jdbc.Driver"
        url="jdbc:jtds:sqlserver://localhost:3306/sailplanner"
        username=user
        password=password
        testOnBorrow="true"
        testOnConnect="true"
        validationQuery="SELECT 1"
        removeAbandoned="true"
        removeAbandonedTimeout="30"
        />
    </Context>
    
  2. 必须将 SQL Server JDBC 驱动程序 (jtds-version.jar) 复制到 TOMCAT_HOME/lib 文件夹(如果还没有的话)

  3. 如果有多个数据源(OLTP1、OLTP2 等),我们将添加多个 &lt;Resource/&gt; 标签

  4. 您将使用以下 JNDI 名称来命名我们的数据源:

    jdbc/Oltp1 – main OLTP database
    jdbc/Oltp2 – secondary database
    
  5. 您将配置数据源,以便连接池知道如何测试连接以及如何处理关闭的连接:

    testOnBorrow="true"
        testOnConnect="true"
        validationQuery="SELECT 1"
        removeAbandoned="true"
        removeAbandonedTimeout="30"
    
  6. 添加新资源或修改现有资源时必须重新启动Tomcat。

application.properties 文件

重要!该文件被部署为类路径的一部分 (src/main/resources/application.properties),而不是具有外部化属性的外部 application.properties 部署到 TOMCAT_HOME/conf

  1. 您将在此文件中添加以下属性:

    spring.datasource.jndi-name=java:comp/env/jdbc/Oltp1
    
  2. 如果存在,您需要从 application.properties 中删除所有其他 spring.datasource 属性

  3. 重要!您必须从外部配置文件中删除所有 spring.datasource 属性(部署到 COMCAT_HOME/conf 的 application.properties)

【讨论】:

    【解决方案3】:

    您应该将数据库连接属性放在属性文件中,并且根据您的要求,您可以更改属性文件中的值

            <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClass" value="${jdbc.driverClassName}" />
            <property name="jdbcUrl" value="${jdbc.databaseurl}" />
            <property name="user" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
    
        </bean>
    

    属性文件

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.databaseurl=jdbc:mysql://localhost:3306/sailplanner
    
    jdbc.username=root
    jdbc.password=root
    

    并将此文件包含在 xml 中使用

    <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="ignoreUnresolvablePlaceholders" value="true" />
            <property name="locations">
                <list>
                    <value>classpath:applications.properties</value> 
    
                </list>
            </property>
        </bean>
    

    【讨论】:

    • 在使用 OpenShift 时,我喜欢将数据库连接放入 tomcat 的 context.xml 中,而不是在应用程序中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 2022-12-11
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多