我有一个dao包,其中我的所有DaoClasses都实现了以下方法:
public class XDaoImpl implements XDao {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Override
public void saveOrUpdate(MyObject o) {
//Transaction trans = getSessionFactory().getCurrentSession().beginTransaction();
getSessionFactory().getCurrentSession().saveOrUpdate(o);
//trans.commit();
}
}
如果我运行我的应用程序,则需要很多时间将对象存储到数据库中。我相信这是因为我为每个对象创建了一个新的Transaction。所以我尝试使用Spring Framework的基于xml的声明式事务实现。但到目前为止它对我不起作用:
<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-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/resources/spring/config/database.properties</value>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="hibernate3SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>/resources/spring/config/A.hbm.xml</value>
<value>/resources/spring/config/B.hbm.xml</value>
<value>/resources/spring/config/C.hbm.xml</value>
<value>/resources/spring/config/D.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="dBo" class="com.model.bo.DBoImpl">
<property name="dDao" ref="dDao" />
<property name="bDao" ref="bDao" />
</bean>
<bean id="dDao" class="com.model.dao.dDaoImpl">
<property name="sessionFactory" ref="hibernate3SessionFactory" />
</bean>
<bean id="aBo" class="com.model.bo.aBoImpl">
<property name="aDao" ref="aDao" />
<property name="bDao" ref="bDao" />
<property name="cDao" ref="cDao" />
</bean>
<bean id="bDao" class="com.model.dao.bDaoImpl">
<property name="sessionFactory" ref="hibernate3SessionFactory" />
</bean>
<bean id="cDao" class="com.model.dao.cDaoImpl">
<property name="sessionFactory" ref="hibernate3SessionFactory" />
</bean>
<bean id="aDao" class="com.model.dao.aDaoImpl">
<property name="sessionFactory" ref="hibernate3SessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernate3SessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="boMethods" expression="execution(* com.model.bo.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="boMethods" />
</aop:config>
</beans>
引发的错误:
class org.springframework.beans.factory.BeanCreationException错误 创建名为&#39; dataSource&#39;的bean在类路径资源中定义 [resources / spring / config / BeanLocations.xml]:BeanPostProcessor之前 bean的实例化失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 用名字创建bean &#39; org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor#0&#39 ;: 无法解析对bean的引用&#39; boMethods&#39;设置bean时 财产&#39;切入点&#39 ;;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 用名称创建bean#boMethods&#39;:bean的实例化失败; 嵌套异常是java.lang.NoClassDefFoundError: 组织/ AspectJ的/韦弗/ BCException
public class DBoImpl implements DBo {
private DDao dDao;
private BDao bDao;
public DDao getDDao() {
return dDao;
}
public void setDDao(DDao dDao) {
this.dDao = dDao;
}
public BDao getbDao() {
return bDao;
}
public void setbDao(BDao bDao) {
this.bDao = bDao;
}
public D add(D r) {
dDao.saveOrUpdate(r);
return r;
}
}
答案 0 :(得分:0)
您的类路径中似乎没有AOP jar文件。
请参阅: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html
查找“10.2.1启用@AspectJ支持”部分
答案 1 :(得分:0)
使用AOP进行交易行为很好。我建议看一下annotation based configuration(注意<tx:annotation-driven transaction-manager="transactionManager" />
),这可能会更直接一点。
此外,我会谨慎使用此配置。事务意味着封装原子工作单元。有一个类似saveOrUpdate(EntityType entity)
的方法我认为你的原子工作单元正在保存一个实体,而不是保存n个实体。事务运行的时间越长,就越有可能陷入死锁。
我建议创建一个类似public void saveOrUpdateInBatch(MyObject ... objects)的方法。这个名字暗示着你正在处理一个处理许多元素的原子操作。您也可以为批量插入优化此方法。