最近用ssh框架(struts2+spring+hibernate)开发一个查询系统,一张表对应一个查询页面,一共大概10张表,10张表之间没联系,但10张表都通过外键与另一张主表相关联。 表里面也没什么数据
tomcat跑起来后,我查询7、8次后就慢了,并且一直停在那里,我设置断点后,发现它停在: Query query = createQuery(session, sql, null);那里,但没有错误信息输出,所在的方法是分页查询方法:
/**
* 分页查询
* @param sql 查询的sql语句
* @param startRow 分页开始显示的纪录数
* @param pageSize 每页显示的数据条数
* @return
*/
public List find(final String sql,final int startRow,final int pageSize) {
log.debug("querying with no parameter");
try{
List result = this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = createQuery(session, sql, null);
query.setFirstResult(startRow);
query.setMaxResults(pageSize);
return query.list();
}
});
if (result == null || result.size()==0) {
log.debug("query successful, no result found");
}
else {
log.debug("query successful, result size: " + result.size());
}
return result;
}
catch(RuntimeException e){
log.error("query failed", e);
throw e;
}
} ssh框架的数据库以及事务配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@210.1.4.38:1521:sscdev"/>
<property name="username" value="ssemem"/>
<property name="password" value="ssemem"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="nativeJdbcExtractor" lazy-init="true" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>
<bean id="lobHandler" lazy-init="true" class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<!-- 一些映射文件的配置-->
<value>com/mem/entity/MmYcMembers.hbm.xml</value>
<value>com/mem/entity/MmItMainOfferMachine.hbm.xml</value>
<value>com/mem/entity/MmItMainInfo.hbm.xml</value>
<value>com/mem/entity/MmItMainOfferDatabase.hbm.xml</value>
<value>com/mem/entity/MmItMainCounterSystem.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.show_sql=false
hbm2ddl.auto=update
hibernate.dialect=org.hibernate.dialect.OracleDialect
</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>mainMachineService</value>
</list>
</property>
</bean>
</beans>
不知道什么原因,有没有人可以帮助我;
tomcat跑起来后,我查询7、8次后就慢了,并且一直停在那里,我设置断点后,发现它停在: Query query = createQuery(session, sql, null);那里,但没有错误信息输出,所在的方法是分页查询方法:
/**
* 分页查询
* @param sql 查询的sql语句
* @param startRow 分页开始显示的纪录数
* @param pageSize 每页显示的数据条数
* @return
*/
public List find(final String sql,final int startRow,final int pageSize) {
log.debug("querying with no parameter");
try{
List result = this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query = createQuery(session, sql, null);
query.setFirstResult(startRow);
query.setMaxResults(pageSize);
return query.list();
}
});
if (result == null || result.size()==0) {
log.debug("query successful, no result found");
}
else {
log.debug("query successful, result size: " + result.size());
}
return result;
}
catch(RuntimeException e){
log.error("query failed", e);
throw e;
}
} ssh框架的数据库以及事务配置:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@210.1.4.38:1521:sscdev"/>
<property name="username" value="ssemem"/>
<property name="password" value="ssemem"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="nativeJdbcExtractor" lazy-init="true" class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"/>
<bean id="lobHandler" lazy-init="true" class="org.springframework.jdbc.support.lob.OracleLobHandler">
<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<!-- 一些映射文件的配置-->
<value>com/mem/entity/MmYcMembers.hbm.xml</value>
<value>com/mem/entity/MmItMainOfferMachine.hbm.xml</value>
<value>com/mem/entity/MmItMainInfo.hbm.xml</value>
<value>com/mem/entity/MmItMainOfferDatabase.hbm.xml</value>
<value>com/mem/entity/MmItMainCounterSystem.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.show_sql=false
hbm2ddl.auto=update
hibernate.dialect=org.hibernate.dialect.OracleDialect
</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
<property name="beanNames">
<list>
<value>mainMachineService</value>
</list>
</property>
</bean>
</beans>
不知道什么原因,有没有人可以帮助我;
解决方案 »
- 这是什么格式啊
- Hibernate中对象A无法得到关联对象B的问题
- 我是学VB的,遇到JAVA编写的网页,不会操作了,来问下各位大蛱
- ireport javabean做数据源问题 请高手赐教!
- 大家讨论一下TOMCAT能不能支持日访问量在50万的网站.
- 各位仁兄大哥帮小弟个忙,带bean的j2ee部署完在别的机器运行完好,但是一道我机器就不好了,错误如下
- 关于毕业论文的开题,诚恳发言另外开贴给分
- 100分求毕业论文摘要英文版,急,明天交!!!!!!!!!!!!!!!!!马上给分。
- 请问谁知道哪里有jetspeed的中文资料?
- 面向对象的题目,输出应该是最后一张图,但是我写的在控制台输出没有东西
- 请教大家两个问题~急~~
- 做华为外包有啥好?
conn.close()?
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
get和load可以不用关session
但是query还是要关吧
得到查询总数的代码为:
/**
* 得到查询的总记录数
*/
public int getCount(String queryStr)throws Exception {
String sql = "select count(*) ";
sql += queryStr;
Object result=this.get(sql);
return Integer.parseInt(result.toString());
}
/**
* 查询一条记录
* @param sql 查询语句
* @return
*/
public Object get(String sql)throws Exception {
log.debug("querying with no parameter");
try{
Query query = this.createQuery(this.getSession(), sql, null);
Object result = query.uniqueResult();
if (result == null) {
log.debug("query successful, no result found");
} else {
log.debug("query successful, result found");
}
return result;
}
catch(RuntimeException e){
log.error("query failed", e);
throw e;
}
catch(Exception ex){
throw ex;
}
}
1 已经满了
2 有死锁我认为你的连接没有了,也就是你忘了关闭session来释放连接了。
尽量减少与数据库的交互,
(1)基于xml方式配置不开启事务:
部分配置如下:
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 采用@Transanctional注解方式使用事务 -->
<tx:annotation-driven transaction-manager="txManager" />
<bean id="personService" class="com.yakoo5.service.impl.PersonServiceBean">
<property name="dataSource" ref="dataSource" />
</bean>
<aop:config>
<aop:pointcut expression="execution(* com.yakoo5.service.impl.*.*(..))" id="transanctionPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="transanctionPointcut"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
此配置会拦截所有的com.yakoo5.service.impl包和子包下的所有已采用注解方式配置事务的类的所有get方法。已只读方式访问数据库,且不会开启事务。(2)采用注解方式配置不开启事务
以下是我以前写的一个类,可作参考用:
package com.yakoo5.service.impl;import java.util.List;import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;import com.yakoo5.bean.Person;
import com.yakoo5.service.PersonService;@Transactional
public class PersonServiceBean implements PersonService {
@Resource
private SessionFactory sessionFactory; public void save(Person person) {
sessionFactory.getCurrentSession().persist(person);
} public void update(Person person) {
sessionFactory.getCurrentSession().merge(person);
} public void delete(int personId) {
sessionFactory.getCurrentSession()
.delete(
sessionFactory.getCurrentSession().load(Person.class,
personId));
} @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public Person getPersonById(int personId) {
return (Person) sessionFactory.getCurrentSession().get(Person.class,
personId);
} @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
@SuppressWarnings("unchecked")
public List<Person> queryAll() {
Query q = sessionFactory.getCurrentSession().createQuery("from Person");
return q.list();
}}注意红色部分的配置!已注解方式配置事务时要在bean.xml文件中加入以下配置信息<tx:annotation-driven transaction-manager="txManager" />还有记得引入tx命名空间。
只需在需要不开启事务的方法前加入如下注解即可:
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
<property name="beanNames">
<list>
<value>mainMachineService </value>
</list>
</property>
里,我这里list中的value只写了一个,实际写了很多个,结果就粗心把其中的某个错写成大写了.
不过还是谢谢大家了