gpt4 book ai didi

java - JDBC 连接发布问题

转载 作者:行者123 更新时间:2023-12-04 18:06:01 24 4
gpt4 key购买 nike

我在我的应用程序和应用程序服务器 jbossAS7 中使用 Struts2、Spring3 和 Hibernate4。问题是我已将连接池中的最大限制配置为 20。当 20 个用户访问应用程序时,会出现无法获取托管连接异常。所有 20 个连接仅由 10 个用户使用,每个用户 2 个连接。为什么会这样?

下面是jboss standalone.xml中datasource的配置

<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;
}
}

网络.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 事务管理器是如何管理这些连接的。对于 hibernate 模式,有连接 Release模式,在事务处理后我可以释放连接,但是当我有 spring 事务处理时这似乎不起作用。请帮忙。

最佳答案

您需要在这种环境中使用 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 session 应该自动关闭。

不要忘记配置 Spring JTA Tx Manager

<bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" />

关于java - JDBC 连接发布问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26749896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com