how2j.cn

下载区
文件名 文件大小
c3p0-0.9.1.jar 594k
spring_hibernate.rar 12m

解压rar如果失败,请用5.21版本或者更高版本的winrar

点击下载 winrar5.21

工具版本兼容问题
配置applicationContext.xml,使得其支持c3p0数据库连接池



步骤 1 :

applicationContext.xml

edit
调整applicationContext.xml以使得其支持c3p0数据库连接池
主要是修改数据源database
1. database class
从 org.springframework.jdbc.datasource.DriverManagerDataSource
改为 com.mchange.v2.c3p0.ComboPooledDataSource
2. driverClassName 改为 driverClass
3. url 改为 jdbcUrl
4. username 改为 user
5. 增加c3p0相关配置
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="c" class="com.how2java.pojo.Category"> <property name="name" value="yyy" /> </bean> <bean name="dao" class="com.how2java.dao.CategoryDAO"> <property name="sessionFactory" ref="sf" /> </bean> <bean name="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="ds" /> <property name="mappingResources"> <list> <value>com/how2java/pojo/Category.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true hbm2ddl.auto=update </value> </property> </bean> <bean name="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="admin" /> <!--连接池中保留的最小连接数。--> <property name="minPoolSize" value="10" /> <!--连接池中保留的最大连接数。Default: 15 --> <property name="maxPoolSize" value="100" /> <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <property name="maxIdleTime" value="1800" /> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <property name="acquireIncrement" value="3" /> <!--最大的Statements条数 --> <property name="maxStatements" value="1000" /> <!--初始化10条连接 --> <property name="initialPoolSize" value="10" /> <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 --> <property name="acquireRetryAttempts" value="30" /> <!--每隔60秒发一次心跳信号到数据库,以保持连接的活性 --> <property name="idleConnectionTestPeriod" value="60" /> </bean> </beans>


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2018-04-05 能进行数据库操作,但是启动服务器就报错了,为什么?
阿米妮




能进行操作数据库,但是为什么启动服务器会报错
严重: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ssh09]]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
	at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:142)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5623)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	... 11 more
2018-4-5 22:20:15 org.apache.catalina.startup.HostConfig deployDirectory
严重: Error deploying web application directory D:\tomcat\apache-tomcat-7.0.75-windows-x64\apache-tomcat-7.0.75\webapps\ssh09
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ssh09]]
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:903)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)
2018-4-5 22:20:15 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\tomcat\apache-tomcat-7.0.75-windows-x64\apache-tomcat-7.0.75\webapps\ssh09 has finished in 3,006 ms
2018-4-5 22:20:15 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
2018-4-5 22:20:15 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
2018-4-5 22:20:15 org.apache.catalina.startup.Catalina start
信息: Server startup in 4241 ms
严重: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ssh09]]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:162)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
	at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:142)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5623)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
	... 11 more
2018-4-5 22:20:15 org.apache.catalina.startup.HostConfig deployDirectory
严重: Error deploying web application directory D:\tomcat\apache-tomcat-7.0.75-windows-x64\apache-tomcat-7.0.75\webapps\ssh09
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/ssh09]]
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:903)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)
2018-4-5 22:20:15 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\tomcat\apache-tomcat-7.0.75-windows-x64\apache-tomcat-7.0.75\webapps\ssh09 has finished in 3,006 ms
2018-4-5 22:20:15 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
2018-4-5 22:20:15 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
2018-4-5 22:20:15 org.apache.catalina.startup.Catalina start
信息: Server startup in 4241 ms


2 个答案

月步
答案时间:2019-08-28
没导jar包吧~

gsdad_white
答案时间:2019-02-18
想问一下解决了吗?



回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到









提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 JAVA 框架-Spring+Hibernate-使用c3p0连接池 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图