【问题标题】:override spring batch admin to use mysql database覆盖spring批处理管理员以使用mysql数据库
【发布时间】:2014-03-20 13:10:05
【问题描述】:

我正在尝试在 spring 批处理管理中使用 mysql 数据库而不是默认的 HSQL。根据文档

http://docs.spring.io/spring-batch-admin/reference/reference.xhtmlUsing jndi datasource with spring batch admin

我将env-context.xml 复制到src/main/resources/META-INF/batch/override/manager/env-context.xml 并将其配置值从

<value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>

 <value>classpath:batch-mysql.properties</value>

以下是我的完整配置。

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">

    <!--  Use this to set additional properties on beans at run time -->
    <bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
                <value>classpath:batch-default.properties</value>
                <value>classpath:batch-mysql.properties</value>
            </list>
        </property>
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="ignoreUnresolvablePlaceholders" value="false" />
        <property name="order" value="1" />
    </bean>

</beans>

我还尝试将 data-source-context.xml 复制到同一文件夹并将其配置更改为 mysql

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost/batch" />
        <property name="username" value="root" />
        <property name="password" value="root" />
        <property name="testWhileIdle" value="true"/>
        <property name="validationQuery" value="SELECT 1"/>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!--  Initialise the database if enabled: -->
    <jdbc:initialize-database data-source="dataSource" enabled="false" ignore-failures="DROPS">
        <jdbc:script location="classpath*:/org/springframework/batch/core/schema-drop-mysql.sql"/>
        <jdbc:script location="classpath:/org/springframework/batch/core/schema-mysql.sql"/>
        <jdbc:script location="classpath:/business-schema-mysql.sql"/>
    </jdbc:initialize-database>

</beans>

但它仍然使用 hsql 数据库?如何覆盖默认配置以使用mysql数据库?

【问题讨论】:

    标签: java spring spring-mvc spring-batch spring-batch-admin


    【解决方案1】:

    您不应替换 &lt;value&gt;classpath:batch-${ENVIRONMENT:hsql}.properties&lt;/value&gt;。相反,传入一个环境变量ENVIRONMENT 设置为mysql。这应该会导致所有适当的组件选择正确的数据库。您可以在此处阅读有关该功能的更多信息:http://docs.spring.io/spring-batch-admin/reference/infrastructure.html#Environment_Settings

    【讨论】:

      【解决方案2】:

      如果您想尝试仅使用注释而不使用任何 xml 配置 - 试试这个

      HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
      jpaVendorAdapter.setDatabase(Database.MYSQL); 
      

      它正在工作。我的代码在这里可用 - http://github.com/sidnan/spring-batch-example

      【讨论】:

        【解决方案3】:

        我能够通过以下步骤使用上述方法建立连接

        首先,我将env-context.xml复制到src/main/resources/META-INF/batch/override/manager/env-context.xml

        <?xml version="1.0" encoding="UTF-8"?>`
        <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.xsd">`
        
            <!--  Use this to set additional properties on beans at run time -->
            <bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                    <list>
                        <value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
                        <value>classpath:batch-default.properties</value>
                        <value>classpath:batch-${ENVIRONMENT:sqlserver}.properties</value>
                    </list>
                </property>
                <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
                <property name="ignoreResourceNotFound" value="true" />
                <property name="ignoreUnresolvablePlaceholders" value="false" />
                <property name="order" value="1" />
            </bean>
        
        </beans>
        

        之后,将以下条目放入resources下sql server的batch-sqlserver.properties

        # Default placeholders for database platform independent features 
        batch.remote.base.url=http://localhost:8080/spring-batch-admin-sample
        # Non-platform dependent settings that you might like to change
        
        batch.jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
        batch.jdbc.url=jdbc:sqlserver://localhost;databaseName=batchrepo
        batch.jdbc.user=la
        batch.jdbc.password=la
        atch.jdbc.testWhileIdle=true
        batch.data.source.init=false
        batch.jdbc.validationQuery=
        
        batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.SqlServerMaxValueIncrementer
        batch.lob.handler.class=org.springframework.jdbc.support.lob.DefaultLobHandler
        batch.database.incrementer.parent=columnIncrementerParent
        batch.grid.size=2
        batch.jdbc.pool.size=6
        batch.verify.cursor.position=true
        batch.isolationlevel=ISOLATION_SERIALIZABLE
        batch.initializer.enabled=false
        

        由于我的表已经在数据库中创建,我跳过了这些条目: #batch.drop.script=/org/springframework/batch/core/schema-drop-sqlserver.sql #batch.schema.script=/org/springframework/batch/core/schema-sqlserver.sql #batch.business.schema.script=business-schema-sqlserver.sql

        最后,通过batch.initializer.enabled=false,我终于能够建立连接。

        我可以在我的管理应用程序中监控工作以及午餐新工作。这些启动的作业也出现在数据库中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-04
          • 2013-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多