日期:2014-05-16  浏览次数:20773 次

使用Hibernate与MySQL时遇到的一个问题,记录下来.
1.错误描述:
警告: SQL Error: 0, SQLState: 08S01
2009-4-13 16:21:23 org.hibernate.util.JDBCExceptionReporter logExceptions
严重: The driver was unable to create a connection due to an inability to establish the client portion of a socket.
This is usually caused by a limit on the number of sockets imposed by the operating system. This limit is usually configurable.
For Unix-based platforms, see the manual page for the 'ulimit' command. Kernel or system reconfiguration may also be required.
For Windows-based platforms, see Microsoft Knowledge Base Article 196271 (Q196271).
2009-4-13 16:21:23 org.hibernate.util.JDBCExceptionReporter logExceptions

2.解决方案:
上述问题是在大批量插入数据的情况下出现的,使用的数据源配置如下:
<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/test</value>
		</property>
		<property name="username">
			<value>username</value>
		</property>
		<property name="password">
			<value>password</value>
		</property>
</bean>

通过错误信息初步判断是数据库的连接数不够用的,试着换了一下连接池,问题解决:
<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="jdbcUrl">
			<value>jdbc:mysql://localhost:3306/test</value>
		</property>
		<property name="properties">
			<props>
				<prop key="c3p0.minPoolSize">2</prop>
				<prop key="c3p0.maxPoolSize">50</prop>
				<prop key="c3p0.timeout">5000</prop>
				<prop key="c3p0.max_statement">100</prop>
				<prop key="c3p0.testConnectionOnCheckout">true</prop>
				<prop key="user">root</prop>
				<prop key="password">123456</prop>
			</props>
		</property>
	</bean>

至于更深层的原因,有时间继续研究,有新的结果再继续和大家讨论.
1 楼 魔力猫咪 2009-04-13  
很正常。因为“DriverManagerDataSource并没有提供连接池的功能,只能作简单的连接测试”。也就是说其实它就是一个JDBC连接,但是表现成一个数据源让你用来测试的。记住下次选Spring组件的时候多看一下说明书。
2 楼 kim_miao 2009-04-14  
魔力猫咪 写道

很正常。因为“DriverManagerDataSource并没有提供连接池的功能,只能作简单的连接测试”。也就是说其实它就是一个JDBC连接,但是表现成一个数据源让你用来测试的。记住下次选Spring组件的时候多看一下说明书。

多谢指导!