Spring问题汇总

来源:互联网 发布:c罗梅西 知乎 编辑:程序博客网 时间:2024/05/18 07:40

与JBoss集成:

由于历史原因,JBoss和Spring的集成并那么理想, 需要加上其他包,以及配置web.xml
<context-param>      <param-name>contextClass</param-name>      <param-value>          org.jboss.spring.vfs.context.VFSXmlWebApplicationContext      </param-value>  </context-param>

需要下载jboss-spring-subsystem-as7-nodeps-2.0.1.Final.zip 中的module-deployer\org\jboss\snowdrop\main\snowdrop-vfs.jar
参阅 http://www.jboss.org/snowdrop/downloads keyword: snowdrop


与Hibernate集成:

懒加载

session在service调用完毕之后关闭, 导致在jsp无法获取当前session, 在web.xml里面加入以下配置, 使session在view情况下开起, 以满足lazy的需求
在配置mapping的时候,需要注意filter的顺序, 该filter的优先级可以为最高
<filter> <filter-name>openSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class></filter> <filter-mapping><filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>

事务处理

Hibernate默认情况下,在select, update, insert, delete之前都会将脏数据flush掉, 在某些情况下,我们并不需要这样的场景,需要将flushmode为manual或者commit
bean.xml  在hibernateTemplate中注入flushMode的值, 是Hibernate的父类HibernateAccessor中的一个属性
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory" /><property name="flushMode" value="3"></property></bean>

web.xml, 在web应用中,OpenSessionInViewFilter中的flushMode参数会将hibernateTemplate中的覆盖掉.
<filter> <filter-name>openSessionInViewFilter</filter-name><filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class><init-param>          <param-name>flushMode</param-name>          <param-value>COMMIT</param-value></init-param></filter>

连接超时

在一段时间不适用应用之后,初次访问应用会遇到以下异常,但是刷新之后异常消失
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed: org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:599)org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:374)org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:263)org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:101)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)$Proxy24.validUser(Unknown Source)active.security.servlet.LoginServlet.doPost(Unknown Source)javax.servlet.http.HttpServlet.service(HttpServlet.java:637)javax.servlet.http.HttpServlet.service(HttpServlet.java:717)active.security.servlet.DelegatingServletProxy.service(Unknown Source)org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)active.security.filter.CharsetEncodingFilter.doFilter(Unknown Source)

可能是由于数据源的配置错误导致, 如下配置c3p0:
beans.xml
<bean id="dataSource" destroy-method="close"class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClassName}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /><property name="breakAfterAcquireFailure" value="true" /></bean>

参数解释:
<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间, default:3 --><property name="initialPoolSize" value="${c3p0.initialPoolSize}" /><!--连接池中保留的最小连接数 --><property name="minPoolSize" value="${c3p0.minPoolSize}" /><!--连接池中保留的最大连接数, default:15 --><property name="maxPoolSize" value="${c3p0.maxPoolSize}" /><!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃, default:0 --><property name="maxIdleTime" value="${c3p0.maxIdleTime}" /><!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数, default:3 --><property name="acquireIncrement" value="${c3p0.acquireIncrement}" /><!--每n秒检查所有连接池中的空闲连接, default:0 --><property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" /><!--定义在从数据库获取新连接失败后重复尝试的次数, default:30 --><property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}" /><!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常.但是数据源仍有效保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试获取连接失败后该数据源将申明已断开并永久关闭, default:false --><property name="breakAfterAcquireFailure" value="${c3p0.breakAfterAcquireFailure}" /><!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量.但由于预缓存的statements属于单个connection而不是整个连接池,所以设置这个参数需要考虑到多方面的因素.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭, default:0 --><property name="maxStatements" value="${c3p0.maxStatements}" /><!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable等方法来提升连接测试的性能, default:false --><property name="testConnectionOnCheckout" value="${c3p0.testConnectionOnCheckout}" />



原创粉丝点击