我的应用程序中有2个不同的数据库,(我正在使用spring Mvc``hibernate
)一个在localhost中,另一个是远程数据库。我是否可以在dispacher Servlet
中声明两个数据源,如下所示:
<context:component-scan base-package="com.testcode.controller" />
<context:component-scan base-package="com.testcode.services" />
<context:component-scan base-package="com.testcode.dao" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix">
<value>/WEB-INF/views/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!-- declare beans -->
<bean id="serverService" class="com.testcode.services.ServerServiceImpl">
<property name="serverDao" ref="serverDao"></property>
</bean>
<bean id="serverDao" class="com.testcode.dao.ServerdaoImpl">
<property name="sessionFactory" ref="SessionFactory2" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/database1" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.1.8:3306/database2" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="SessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.testcode.domain.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="SessionFactory2"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="annotatedClasses">
<list>
<value>com.testcode.domain.Server</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.current_session_context_class ">thread</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/images/**" location="/resources/core/images/" />
<mvc:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<bean id="transactionManager2"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory2" />
</bean>
<tx:annotation-driven/>
<tx:annotation-driven transaction-manager="transactionManager" />
<tx:annotation-driven transaction-manager="transactionManager2" />
当我尝试将服务器插入远程数据库中的表时出现此错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread
这个问题是由于2个定义的数据源还是什么?
修改:
这是serverDaoImpl
@Repository("serverDao")
public class ServerdaoImpl implements ServersDao{
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf){
this.sessionFactory = sf;
}
@Override
public void insertServer(Server server) {
Session session = this.sessionFactory.getCurrentSession();
session.persist(server);
}
@SuppressWarnings("unchecked")
@Override
public List<Server> getServerList() {
Session session = this.sessionFactory.getCurrentSession();
List<Server> serverList = session.createQuery("from servers").list();
return serverList;
}
@Override
public void updateServer(Server server) {
Session session = this.sessionFactory.getCurrentSession();
session.update(server);
}
这是serverServiceImpl
:
@Service("serverService")
public class ServerServiceImpl implements ServerService {
private ServersDao serverdao;
public void setServerDao(ServersDao serverdao) {
this.serverdao = serverdao;
}
@Override
@Transactional
public void insertServer(Server server) {
this.serverdao.insertServer(server);
}
@Override
@Transactional
public List<Server> getServerList() {
return this.serverdao.getServerList();
}
答案 0 :(得分:1)
您尚未在<tx:annotation-driven transaction-manager="transactionManager" />
只需确保您也正确定义了
<mvc:annotation-driven />
<context:component-scan base-package="" />
还要检查hibernate.properties文件中是否添加了<beans:prop key="hibernate.current_session_context_class">thread</beans:prop>
请在您的服务或DAO方法上指定@Transactional
答案 1 :(得分:0)
我已经解决了这个问题,现在我可以同时使用这两个数据源了。
问题是TransactionManager
和TransactionManager2
我在serverService中添加了@Transactional(value="TransactionManager2")
。
希望这将有助于将来的某些人。