我在我的应用程序和应用程序服务器jbossAS7中使用Struts2,Spring3和Hibernate4。问题是我已将连接池中的最大限制配置为20.当20个用户点击该应用程序时,无法获得托管连接异常。所有20个连接仅由10个用户使用,每个用户有2个连接。为什么会这样?
以下是jboss standalone.xml中数据源的配置
<datasource jta="false" jndi-name="java:/comp/env/jdbc/DB_NAME" pool-name="DB_POOL" enabled="true" use-java-context="true" use-ccm="false">
<connection-url>jdbc:log4jdbc:postgresql://localhost:5432/test_db</connection-url>
<driver-class>net.sf.log4jdbc.sql.jdbcapi.DriverSpy</driver-class>
<driver>log4jdbc</driver>
<pool>
<min-pool-size>2</min-pool-size>
<max-pool-size>20</max-pool-size>
</pool>
<security>
<user-name>postgres</user-name>
<password>postgres</password>
</security>
<validation>
<validate-on-match>false</validate-on-match>
<background-validation>false</background-validation>
</validation>
<statement>
<prepared-statement-cache-size>0</prepared-statement-cache-size>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
Spring事务管理器配置:
<bean id="transactionManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED,-java.lang.Exception</prop>
</props>
</property>
</bean>
<bean id="loginDTO" class="com.tcs.oss.tims.loginManagement.dto.LoginDTO" />
<bean id="loginAction" class="com.tcs.oss.tims.loginManagement.action.LoginAction" scope="prototype">
<property name="loginDAO" ref="loginDAO" />
<property name="loginDTO" ref="loginDTO" />
<property name="roleBasedAction" ref="roleBasedAction"></property>
</bean>
<bean id="loginDAOImpl" class="com.tcs.oss.tims.loginManagement.dao.daoImpl.LoginDAOImpl" scope="prototype">
<property name="sessionfactory" ref="sessionFactory" />
</bean>
<bean id="loginDAO"parent="baseTransactionProxy" scope="prototype">
<property name="target"> <ref bean="loginDAOImpl"/> </property>
</bean>
Hibernate配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
</props>
</bean>
数据源配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/comp/env/jdbc/TIMS_DB"/>
</bean>
示例代码:
public void insertIntoInstance(String id) throws Exception {
try {
Session session = sessionfactory.getCurrentSession(); // Where the sessionfactory is from Dependency Injection
Query insertQry = session.createSQLQuery("insert into table(id)values(id)");
insertQry.executeUpdate();
} catch (Exception e) {
throw e;
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/SpringBeans.xml</param-value>
</context-param>
<servlet>
<servlet-name>InitializerServlet</servlet-name>
<servlet-class>com.tcs.oss.tims.utilities.InitializerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter-mapping>
<filter-name>CASFilter</filter-name>
<url-pattern>/General/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>CASFilter</filter-name>
<filter-class>edu.yale.its.tp.cas.client.filter.CASFilter</filter-class>
<init-param>
<param-name>edu.yale.its.tp.cas.client.filter.loginUrl</param-name>
<param-value>${CAS_LOGIN_URL}</param-value>
</init-param>
<init-param>
<param-name>edu.yale.its.tp.cas.client.filter.validateUrl</param-name>
<param-value>${CAS_PROXY_VALIDATE_URL}</param-value>
</init-param>
<init-param>
<param-name>edu.yale.its.tp.cas.client.filter.serviceUrl</param-name>
<param-value>${LOGIN_URL}</param-value>
</init-param>
<init-param>
<param-name>edu.yale.its.tp.cas.client.filter.wrapRequest</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
我不确定spring事务管理器如何管理连接。对于只是休眠,有连接释放模式,在事务之后我可以释放连接但是当我有spring事务时似乎不起作用。请帮忙。
答案 0 :(得分:0)
您需要在这种环境中使用JTA事务管理器。
当您使用成熟的应用程序服务器时,它已经有一个内置的事务管理器,它与您设置的数据源集成(至少在您设置jta =“true”时)。另外一定要使用:
<prop key="hibernate.transaction.jta.platform">
org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform
</prop>
<prop key="hibernate.transaction.factory_class">
org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory
</prop>
提交事务时,您的hibernate会话应自动关闭并释放相关的数据库连接。
不要忘记配置Spring JTA Tx Manager
<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" />