1

我必须在我的应用程序中管理多个数据库我的配置文件应用程序是

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="fr.tessa.jee.webmanager" />

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

    <!-- misc -->
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Configure the multipart resolver -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="1000000" />
    </bean>

    <!-- Configures Hibernate - Database Config -->
    <import resource="dbconfig/db-config.xml" /> 
    <import resource="dbconfig/validationdb-config.xml" />

</beans>

我的“db-config.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

    <!-- Hibernate Data Source -->
    <bean id="teoDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
        <property name="url"><value>jdbc:mysql://127.0.0.1/teodijon</value></property>
        <property name="username"><value>root</value></property>
        <property name="password"><value</value></property>
    </bean>    
     <!-- Hibernate Session Factory -->
    <bean id="teoSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="teoDataSource"/></property>
        <property name="packagesToScan" value="fr.tessa.jee.webmanager.model" />
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
        </property>
    </bean> 
    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) HibernateTransactionManager -->
    <tx:annotation-driven transaction-manager="teoTransactionManager"/>
    <bean id="teoTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory"><ref local="teoSessionFactory"/></property>
    </bean>
</beans>

和“validationdb-config.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

    <!-- Hibernate Data Source -->
    <bean id="validationDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
        <property name="url"><value>jdbc:mysql://127.0.0.1/validationdb</value></property>
        <property name="username"><value>root</value></property>
        <property name="password"><value></value></property>
    </bean>    
     <!-- Hibernate Session Factory -->
    <bean id="validationSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="validationDataSource"/></property>
        <property name="packagesToScan" value="fr.tessa.jee.webmanager.validation.model" />
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
        </property>
    </bean> 
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) HibernateTransactionManager -->
<tx:annotation-driven transaction-manager="validationTransactionManager"/>
    <bean id="validationTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory"><ref local="validationSessionFactory"/></property>
    </bean>
</beans>

我在包 fr.tessa.jee.webmanager.validation.model 中有用户类,在包 fr.tessa.jee.webmanager.model 中有客户端;问题是,当我尝试获取用户列表时出现错误,例如没有为当前线程找到会话但是对于客户端列表很好,当我开始在 app-config 中导入“validationdb-config.xml”时

<import resource="dbconfig/validationdb-config.xml" />
<import resource="dbconfig/db-config.xml" /> 

当我尝试获取客户列表时出现错误

我的配置有什么问题?请帮忙 :(

4

1 回答 1

1

我找到了问题,问题出在我的服务中的事务方法中,我必须像这样指定事务管理器的 ID

@Transactional(readOnly = true, value="teoTransactionManager")
public List<Client> getClientList() {
    return clientDAO.getClients();
}

当我开始时,我只输入了@Transactional(readOnly = true)

于 2013-02-15T08:59:31.010 回答