8

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

http://docs.spring.io/spring-batch-admin/reference/reference.xhtml将 jndi 数据源与 spring 批处理管理一起使用

我复制env-context.xmlsrc/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 数据库?

4

3 回答 3

2

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

于 2014-03-21T16:44:15.643 回答
0

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

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

这是工作。我的代码可在此处获得 - http://github.com/sidnan/spring-batch-example

于 2014-08-15T01:48:02.310 回答
0

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

首先,我复制env-context.xmlsrc/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>

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

# 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/批处理/核心/schema-sqlserver.sql #batch.business.schema.script=business-schema-sqlserver.sql

最后,有了batch.initializer.enabled=false,我终于能够建立联系。

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

于 2018-02-01T20:28:50.383 回答