与tomcat 7建立数据库连接

时间:2015-12-21 14:09:38

标签: java mysql spring tomcat

我有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中设置了数据库连接?

3 个答案:

答案 0 :(得分:2)

您必须使用JNDI创建一个dataSource:

首先,在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" />

答案 1 :(得分:1)

对于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等),我们将添加多个<Resource/>标记

  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。

  7. application.properties文件

    重要!此文件作为类路径(src / main / resources / application.properties)的一部分进行部署,而不是部署到TOMCAT_HOME / conf的外部属性的外部application.properties

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

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

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

答案 2 :(得分:0)

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

        <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>
相关问题