2、Spring之连接池使用
<!--Mysql-->datasource.driverClassName=com.mysql.jdbc.Driverdatasource.url=jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=UTF-8datasource.username=rootdatasource.password=root#hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect<!--Oracle-->#datasource.driverClassName=oracle.jdbc.driver.OracleDriver#datasource.url=jdbc:oracle:thin:@Jack:1521:db_test#datasource.username=accp#datasource.password=accp#hibernate.dialect=org.hibernate.dialect.Oracle9Dialect<!--SqlServer2000-->#jdbc.driverClassName=com.microsoft.jdbc.sqlserver.SQLServerDriver#jdbc.url=jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db_test#jdbc.username=sa#jdbc.password=123#hibernate.dialect=org.hibernate.dialect.SQLServerDialect<!--SqlServer2005-->#jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver#jdbc.url=jdbc:sqlserver://localhost:1434;DatabaseName=db_test;SelectMethod=cursor#jdbc.username=sa#jdbc.password=123#hibernate.dialect=org.hibernate.dialect.SQLServerDialect
?(3)、配置Spring配置文件applicationContext-resource.xml:
<!--加载数据配置文件对象--><bean id="propertyConfigurer"destroy-method="close"><!-- 指定连接数据库的驱动 --><property name="driverClassName"value="${datasource.driverClassName}" /><!-- 指定连接数据库的URL --><property name="url" value="${datasource.url}" /><!-- 指定连接数据库的用户名 --><property name="username" value="${datasource.username}" /><!-- 指定连接数据库的密码 --><property name="password" value="${datasource.password}" /><!-- 连接池启动时的初始值 --><property name="initialSize" value="4" /><!-- 连接池的最大值 --><property name="maxActive" value="100" /><!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止--><property name="maxIdle" value="2" /><!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --><property name="minIdle" value="1" /></bean>
?(4)、web.xml中配置,让Spring启动时处理对应配置文件即可。
<!-- spring配置文件的开始 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/applicationContext-resource.xmlclasspath:/applicationContext-dao.xmlclasspath:/applicationContext-service.xmlclasspath:/applicationContext-action.xml<!-- WEB-INF/applicationContext-action.xml --></param-value></context-param>
?
2、采用Proxool连接池:
(1)、加入相关jar包:proxool-0.9.1.jar,proxool-cglib.jar。
(2)、通过xml配置文件配置数据库相关参数,暂且命名为:proxool.xml,并放到webroot目录下的WEB-INF目录下,内容如下,此处以MySql为例:
<?xml version="1.0" encoding="UTF-8"?><something-else-entirely><proxool><alias>db_test</alias><driver-url>jdbc:mysql://127.0.0.1:3306/db_test</driver-url><driver-class>com.mysql.jdbc.Driver</driver-class><driver-properties><property name="user" value="root" /><property name="password" value="root" /></driver-properties><maximum-new-connections>10</maximum-new-connections><prototype-count>5</prototype-count><test-before-use>true</test-before-use><test-after-use>true</test-after-use><house-keeping-sleep-time>60000</house-keeping-sleep-time><house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql><maximum-connection-count>10</maximum-connection-count><minimum-connection-count>2</minimum-connection-count></proxool></something-else-entirely>
(3)、配置web.xml配置文件,加载连接池配置文件,初始化连接池。
<!--连接池配置--><servlet><servlet-name>ServletConfigurator</servlet-name><servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class><init-param><param-name>xmlFile</param-name><param-value>WEB-INF/proxool.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><!-- 监控proxool连接池的请求路径,请求后会返回一个监控连接池性能的页面 --><servlet> <servlet-name>Admin</servlet-name> <servlet-class> org.logicalcobwebs.proxool.admin.servlet.AdminServlet </servlet-class></servlet><servlet-mapping> <servlet-name>Admin</servlet-name> <url-pattern>/admin</url-pattern></servlet-mapping><!--注意,如果使用了Struts2,则需放行上面的请求,不要拦截,否则得不到监控页面--><!-- struts2,仅仅拦截action或者jsp后缀的请求 --><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>*.action</url-pattern></filter-mapping><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.jsp</url-pattern></filter-mapping>
?(4)、配置Spring配置文件applicationContext-resource.xml:
<bean id="dataSource" name="code">//如果Weblogic中配置数据源不激活,则在使用时会出现以下错误org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [applicationContext-resource.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: While trying to lookup 'jdbc.test' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/test'at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.java:582)at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)at weblogic.servlet.internal.EventsManager.executeContextListener(EventsManager.java:233)at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:190)at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:175)at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1730)at weblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:2740)at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1704)at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:781)at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:213)at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:208)at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:35)at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:70)at weblogic.application.internal.flow.ScopedModuleDriver.start(ScopedModuleDriver.java:212)at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:111)at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:213)at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:208)at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:35)at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:70)at weblogic.application.internal.flow.StartModulesFlow.activate(StartModulesFlow.java:24)at weblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:729)at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:35)at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:258)at weblogic.application.internal.SingleModuleDeployment.activate(SingleModuleDeployment.java:48)at weblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:165)at weblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:79)at weblogic.deploy.internal.targetserver.BasicDeployment.activate(BasicDeployment.java:192)at weblogic.deploy.internal.targetserver.BasicDeployment.activateFromServerLifecycle(BasicDeployment.java:369)at weblogic.management.deploy.internal.DeploymentAdapter$1.doActivate(DeploymentAdapter.java:51)at weblogic.management.deploy.internal.DeploymentAdapter.activate(DeploymentAdapter.java:200)at weblogic.management.deploy.internal.AppTransition$2.transitionApp(AppTransition.java:30)at weblogic.management.deploy.internal.ConfiguredDeployments.transitionApps(ConfiguredDeployments.java:240)at weblogic.management.deploy.internal.ConfiguredDeployments.activate(ConfiguredDeployments.java:169)at weblogic.management.deploy.internal.ConfiguredDeployments.deploy(ConfiguredDeployments.java:123)at weblogic.management.deploy.internal.DeploymentServerService.resume(DeploymentServerService.java:186)at weblogic.management.deploy.internal.DeploymentServerService.start(DeploymentServerService.java:94)at weblogic.t3.srvr.SubsystemRequest.run(SubsystemRequest.java:64)at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)Caused by: javax.naming.NameNotFoundException: While trying to lookup 'jdbc.test' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/test'at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1139)?(3)、配置Spring配置文件applicationContext-resource.xml:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"><property name="jndiName"><value>jdbc.test</value></property><property name="resourceRef"> <value>false</value> </property> <!--可要可不要--><property name="jndiEnvironment"> <props> <prop key="java.naming.provider.url">t3://127.0.0.1:7001</prop> <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop> </props> </property> </bean>?
?
?