Skip to content
This repository has been archived by the owner on Mar 7, 2022. It is now read-only.

Commit

Permalink
修复bug
Browse files Browse the repository at this point in the history
1. 修改设定的数据库连接池。
  • Loading branch information
qyvlik committed Jun 19, 2016
1 parent 213b67b commit 1633b6a
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 129 deletions.
6 changes: 5 additions & 1 deletion SSHE/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
</attributes>
</classpathentry>
<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
11 changes: 11 additions & 0 deletions SSHE/.project
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,24 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.genuitec.eclipse.ast.deploy.core.DeploymentBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.genuitec.eclipse.springframework.springbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.genuitec.eclipse.springframework.springnature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
Expand Down
16 changes: 12 additions & 4 deletions SSHE/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,20 @@
</dependency>
<!-- mysql驱动包 -->

<!-- Druid连接池包 -->
<!-- dbcp -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.12</version>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- dbcp -->

<!-- Druid连接池包 -->
<!-- <dependency> -->
<!-- <groupId>com.alibaba</groupId> -->
<!-- <artifactId>druid</artifactId> -->
<!-- <version>1.0.12</version> -->
<!-- </dependency> -->
<!-- Druid连接池包 -->

<!-- aspectjweaver包 -->
Expand Down
2 changes: 2 additions & 0 deletions SSHE/src/main/java/me/gacl/service/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import me.gacl.dao.UserDaoI;
import me.gacl.model.User;
import me.gacl.service.UserServiceI;

//使用Spring提供的@Service注解将UserServiceImpl标注为一个Service

@Service("userService")
public class UserServiceImpl implements UserServiceI {

Expand Down
187 changes: 76 additions & 111 deletions SSHE/src/main/resources/spring-hibernate.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
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
Expand All @@ -8,122 +11,84 @@ http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">

<!-- JNDI方式配置数据源 -->
<!--
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${jndiName}"></property>
</bean>
-->
<!-- JNDI方式配置数据源 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${jndiName}"></property> </bean> -->

<!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
<!-- 定义一个使用 DBCP 实现的数据源 -->
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
</bean>

<!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />
<!-- 配置hibernate session工厂 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<!-- web项目启动时是否更新表结构 -->
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!-- 系统使用的数据库方言,也就是使用的数据库类型 -->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!-- 是否打印Hibernate生成的SQL到控制台 -->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!-- 是否格式化打印出来的SQL -->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>

<!-- <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
<!-- 自动扫描注解方式配置的hibernate类文件 -->
<property name="packagesToScan">
<list>
<value>me.gacl.model</value>
</list>
</property>

<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 自动扫描hbm方式配置的hibernate文件和.hbm文件 -->
<!-- <property name="mappingDirectoryLocations"> <list> <value>classpath:me/gacl/model/hbm</value>
</list> </property> -->
</bean>

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 配置事务管理器 -->
<bean name="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />
<!-- 注解方式配置事物 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

<!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat" />
</bean>
<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 以如下关键字开头的方法使用事物 -->
<tx:method name="add*" />
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="modify*" />
<tx:method name="edit*" />
<tx:method name="delete*" />
<tx:method name="remove*" />
<tx:method name="repair" />
<tx:method name="deleteAndRepair" />
<!-- 以如下关键字开头的方法不使用事物 -->
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<!-- 其他方法不使用事物 -->
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<!-- 切面,将事物用在哪些对象上 -->
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(* me.gacl.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>

<!-- 配置hibernate session工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<!-- web项目启动时是否更新表结构 -->
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<!-- 系统使用的数据库方言,也就是使用的数据库类型 -->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!-- 是否打印Hibernate生成的SQL到控制台 -->
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!-- 是否格式化打印出来的SQL -->
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
</props>
</property>

<!-- 自动扫描注解方式配置的hibernate类文件 -->
<property name="packagesToScan">
<list>
<value>me.gacl.model</value>
</list>
</property>

<!-- 自动扫描hbm方式配置的hibernate文件和.hbm文件 -->
<!--
<property name="mappingDirectoryLocations">
<list>
<value>classpath:me/gacl/model/hbm</value>
</list>
</property>
-->
</bean>

<!-- 配置事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 注解方式配置事物 -->
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 以如下关键字开头的方法使用事物 -->
<tx:method name="add*" />
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="modify*" />
<tx:method name="edit*" />
<tx:method name="delete*" />
<tx:method name="remove*" />
<tx:method name="repair" />
<tx:method name="deleteAndRepair" />
<!-- 以如下关键字开头的方法不使用事物 -->
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="load*" propagation="SUPPORTS" />
<tx:method name="search*" propagation="SUPPORTS" />
<tx:method name="datagrid*" propagation="SUPPORTS" />
<!-- 其他方法不使用事物 -->
<tx:method name="*" propagation="SUPPORTS" />
</tx:attributes>
</tx:advice>
<!-- 切面,将事物用在哪些对象上 -->
<aop:config>
<aop:pointcut id="transactionPointcut" expression="execution(* me.gacl.service..*Impl.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
</aop:config>

</beans>
3 changes: 2 additions & 1 deletion SSHE/src/main/resources/spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ http://www.springframework.org/schema/context/spring-context-3.0.xsd

<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="me.gacl.dao,me.gacl.service" />


<import resource="spring-hibernate.xml"/>
</beans>
4 changes: 1 addition & 3 deletions SSHE/src/test/java/me/gacl/test/TestHibernate.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ public void before(){
}

@Test
public void testSaveMethod(){
//ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-hibernate.xml"});
//UserServiceI userService = (UserServiceI) ac.getBean("userService");
public void testSaveMethod(){
User user = new User();
user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
user.setName("孤傲苍狼");
Expand Down
20 changes: 11 additions & 9 deletions SSHE/src/test/java/me/gacl/test/TestSpring.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

public class TestSpring {

@Test
public void test(){
//通过spring.xml配置文件创建Spring的应用程序上下文环境
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring.xml");
//从Spring的IOC容器中获取bean对象
UserServiceI userService = (UserServiceI) ac.getBean("userService");
//执行测试方法
userService.test();
}
@Test
public void test() {
// 通过spring.xml配置文件创建Spring的应用程序上下文环境
ApplicationContext ac = new ClassPathXmlApplicationContext(
"classpath:spring.xml");
// 从Spring的IOC容器中获取bean对象

UserServiceI userService = (UserServiceI) ac.getBean("userService");
// 执行测试方法
userService.test();
}
}

0 comments on commit 1633b6a

Please sign in to comment.