Spring+Hibernate搭建SOCKET通讯服务,数据库为oracle,C3P0作为连接池,SOCKET通讯方式为:启动一个线程监听socket连接,然后为每个socket连接创建一个线程,专门负责这个连接的通讯线程,对于传入的数据,通过分析通讯规约调用spring服务进行业务处理,spring服务方法中需要调用数据库操作。现在发现socket客户数量一多起来,就会死锁,执行jstack后结果如下:
"9000-106.39.223.149:4241" prio=6 tid=0x49024c00 nid=0x1a10 in Object.wait() [0x63e4f000..0x63e4fb18]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x07d2a098> (a com.mchange.v2.resourcepool.BasicResourcePool)
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1315)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
- locked <0x07d2a098> (a com.mchange.v2.resourcepool.BasicResourcePool)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
at com.lily.dap.service.socket.command.LoginCommand$$EnhancerByCGLIB$$1a972c56.execute(<generated>)
at com.lily.dap.service.socket.task.TaskExecutor.execute(TaskExecutor.java:48)
at com.lily.dap.service.socket.listen.Handler.run(Handler.java:232)
at java.lang.Thread.run(Thread.java:619)
Locked ownable synchronizers:
- None
"9000-36.59.241.72:35010" prio=6 tid=0x48f2c400 nid=0x188c in Object.wait() [0x63dff000..0x63dffb98]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x07d2a098> (a com.mchange.v2.resourcepool.BasicResourcePool)
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1315)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
- locked <0x07d2a098> (a com.mchange.v2.resourcepool.BasicResourcePool)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:81)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
at com.lily.dap.service.socket.command.LoginCommand$$EnhancerByCGLIB$$1a972c56.execute(<generated>)
at com.lily.dap.service.socket.task.TaskExecutor.execute(TaskExecutor.java:48)
at com.lily.dap.service.socket.listen.Handler.run(Handler.java:232)
at java.lang.Thread.run(Thread.java:619)
Locked ownable synchronizers:
- None
...
经过统计,有1116个线程都是这个输出信息。
c3p0配置如下:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${hibernate.connection.driver_class}"/>
<property name="jdbcUrl" value="${hibernate.connection.url}"/>
<property name="properties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="c3p0.minPoolSize">50</prop>
<prop key="hc3p0.maxPoolSize">200</prop>
<prop key="hc3p0.timeout">100</prop>
<prop key="c3p0.max_statement">50</prop>
<prop key="c3p0.testConnectionOnCheckout">true</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
<prop key="user">${hibernate.connection.username}</prop>
<prop key="password">${hibernate.connection.password}</prop>
</props>
</property>
</bean>
事务配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">
<description>Spring DAO 配置文件</description>
<bean id="dao" class="com.lily.dap.dao.hibernate.HibernateDao">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="batchSize" value="20"/>
</bean>
<!-- Hibernate配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.show_sql=${hibernate.show_sql}
hibernate.format_sql=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
</property>
<property name="packagesToScan" value="com.lily.dap.entity" />
</bean>
<!-- 事务管理器配置,单数据源事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>
spring启动代码如下:
public class SocketMain {
public static void main(String[] args) {
URL url = SocketMain.class.getResource("/config/log4j.properties");
PropertyConfigurator.configure(url);
// url = SocketMain.class.getResource("/config/ehcache.xml");
// CacheManager.create(url);
BeanFactory factory = new ClassPathXmlApplicationContext("classpath:config/applicationContext.xml");
ServiceSocketManager attachmentServiceSocketManager = (ServiceSocketManager)factory.getBean("attachmentServiceSocketManager");
attachmentServiceSocketManager.startService();
ServiceSocketManager serviceSocketManager = (ServiceSocketManager)factory.getBean("serviceSocketManager");
serviceSocketManager.startService();
ServiceSocketManager commandServiceSocketManager = (ServiceSocketManager)factory.getBean("commandServiceSocketManager");
commandServiceSocketManager.startService();
...
请帮忙解决分析一下是什么原因。
问题补充:现在发现和oracle有关,我减小了连接池数量,c3p0.minPoolSize=10,hc3p0.maxPoolSize=20,连接一个本地局域网内的测试oracle,写了个socket客户端大并发测试,没有堵塞,打印连接池信息,如下:
getNumConnectionsDefaultUser - 9
getNumIdleConnectionsDefaultUser - 6
getNumBusyConnectionsDefaultUser - 3
getNumUnclosedOrphanedConnectionsDefaultUser - 0
getStatementCacheNumStatementsDefaultUser - 0
getStatementCacheNumCheckedOutDefaultUser - 0
getStatementCacheNumConnectionsWithCachedStatementsDefaultUser - 0
getNumThreadsAwaitingCheckoutDefaultUser - 0
连接实际运营的数据库,有堵塞,打印连接池信息,如下:
getNumConnectionsDefaultUser - 15
getNumIdleConnectionsDefaultUser - 0
getNumBusyConnectionsDefaultUser - 15
getNumUnclosedOrphanedConnectionsDefaultUser - 0
getStatementCacheNumStatementsDefaultUser - 0
getStatementCacheNumCheckedOutDefaultUser - 0
getStatementCacheNumConnectionsWithCachedStatementsDefaultUser - 0
getNumThreadsAwaitingCheckoutDefaultUser - 296
客户的oracle现在不好重装,请教客户oracle配置有什么问题吗
问题补充:通过查资料,发现可以通过优化C3P0配置缓解这个问题,优化如下:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${hibernate.connection.driver_class}" />
<property name="jdbcUrl" value="${hibernate.connection.url}" />
<property name="user" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
<property name="minPoolSize" value="${c3p0.miniPoolSize}" />
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
<property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
<property name="acquireRetryDelay" value="${c3p0.acquireRetryDelay}"/>
<property name="testConnectionOnCheckin" value="${c3p0.testConnectionOnCheckin}"/>
<property name="automaticTestTable" value="${c3p0.automaticTestTable}"/>
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"/>
<property name="checkoutTimeout" value="${c3p0.checkoutTimeout}"/>
</bean>
对应database.propertites
c3p0.miniPoolSize = 10
c3p0.maxPoolSize = 20
c3p0.initialPoolSize = 10
c3p0.maxIdleTime = 60
c3p0.acquireIncrement = 1
c3p0.acquireRetryAttempts = 30
c3p0.acquireRetryDelay = 100
c3p0.testConnectionOnCheckin = true
c3p0.automaticTestTable = c3p0TestTable
c3p0.idleConnectionTestPeriod = 60
c3p0.checkoutTimeout=3000
这样优化后,有时候也还是会堵塞,但不会一直堵塞
相关推荐
10. **数据库操作**:掌握SQL语言,理解JDBC(Java Database Connectivity)API,熟悉数据库连接池(如C3P0、DBCP、HikariCP),以及ORM(Object-Relational Mapping)框架如Hibernate和MyBatis,对数据库事务和索引...
Java中常见的连接池实现有C3P0、DBCP、HikariCP等,实现时需考虑性能和稳定性。 12. **JAVA 面向Internet的CSCW共享白板**: - CSCW(Computer-Supported Cooperative Work)是支持协同工作的计算机系统。共享白板...
7. **数据库交互**:掌握JDBC基础,事务管理,SQL优化,了解NoSQL数据库如MongoDB,以及连接池的使用(如C3P0、Druid、HikariCP)。 8. **设计模式**:熟悉23种设计模式,并能结合实际场景应用,如工厂模式、单例...
- **数据库连接池**:如C3P0、Druid、HikariCP,理解其作用和配置。 4. **其他** - **多线程**:线程的创建、同步、通信,以及死锁问题的预防和解决。 - **网络编程**:Socket编程,TCP/IP协议栈,HTTP协议。 -...
10. **数据库操作**:JDBC API的使用,SQL语句编写,事务处理(ACID特性),连接池(C3P0、DBCP、HikariCP),以及ORM框架如Hibernate和MyBatis的原理与配置。 11. **Spring框架**:IoC(控制反转)与DI(依赖注入...
- 数据库连接池:C3P0、DBCP、Druid等的配置和使用。 - JPA与Hibernate:ORM框架的原理和优势。 9. **Spring框架**: - Spring Core:依赖注入、AOP、事件驱动、bean生命周期。 - Spring MVC:模型-视图-控制器...
10. **数据库操作**:基本的SQL语法,事务处理,连接池的使用(如C3P0、Druid、HikariCP),以及JDBC操作数据库的方法。 11. **Spring框架**:理解依赖注入(DI)和面向切面编程(AOP)的概念,了解Spring Boot和...
数据库方面,掌握SQL语句的基本操作,熟悉JDBC API,了解连接池如C3P0、Druid、HikariCP的使用。对ORM框架如Hibernate、MyBatis的理解和实践经验也非常重要。 网络编程是Java开发者的基础技能,TCP/IP协议、Socket...
熟悉JDBC API,理解连接池的工作原理,如C3P0、Druid等。 9. **框架知识**:Spring框架的IoC和AOP原理,Spring Boot的快速开发特性,MyBatis或Hibernate的ORM映射,以及Spring Cloud或Dubbo等微服务框架的使用。 ...
- JDBC连接池的配置与使用,如C3P0, HikariCP等。 以上只是Java面试题中可能涉及的一部分关键知识点,实际上面试可能会涵盖更多领域,如设计模式、分布式系统、微服务架构等。准备面试时,全面而深入地理解这些...
12. **数据库相关**:SQL基础知识、JDBC操作、事务处理、连接池的使用(如C3P0、Druid、HikariCP)以及ORM框架(如Hibernate、MyBatis)。 13. **网络编程**:TCP/IP协议基础、Socket编程、HTTP协议以及HTTPS的安全...
13. **数据库相关**:JDBC操作,SQL语言,连接池(如C3P0,HikariCP),ORM框架(如Hibernate,MyBatis)。 14. **网络编程**:TCP和UDP协议,Socket编程,HTTP和HTTPS协议。 15. **数据结构与算法**:链表、树、...
13. **数据库操作**:JDBC基础,连接池的使用(如C3P0、Druid、HikariCP),SQL语句优化,事务的ACID属性。 14. **网络编程**:Socket编程,TCP和UDP的区别,服务器端和客户端的实现,HTTP协议的理解。 15. **并发...
10. **数据库相关**:SQL语句基础、JDBC操作、连接池(C3P0、Druid、HikariCP)、ORM框架(Hibernate、MyBatis)等。 11. **网络编程**:TCP/IP协议、HTTP协议、Socket编程以及网络编程在Java中的实现。 12. **...
了解数据库连接池的使用,如C3P0、Druid等。 13. **网络编程**:TCP/IP协议、HTTP协议的基本原理,以及Socket编程。 14. **Java新特性**:随着Java版本的更新,一些新的特性和功能不断引入,例如Lambda表达式、...
"TpVersion2"可能包含了数据库连接池的使用,如C3P0或HikariCP,以优化数据库资源的管理。此外,ORM(Object-Relational Mapping)框架如Hibernate或MyBatis可能被用来简化SQL查询和对象之间的映射,提高开发效率。 ...