**spring+hibernate + c3p0 代码整合 **
来源:互联网 发布:手游运营数据分析 编辑:程序博客网 时间:2024/06/05 06:26
spring+hibernate + c3p0 代码整合
相关java类
DBconn.java
import java.beans.PropertyVetoException;import java.io.PrintWriter;import java.sql.*;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DBconn implements DataSource { private static Connection conn; private static final DBconn instance=new DBconn(); private static ComboPooledDataSource cpds=new ComboPooledDataSource(true); static{ try { cpds.setDriverClass("oracle.jdbc.OracleDriver"); } catch (PropertyVetoException e) { // TODO Auto-generated catch block e.printStackTrace(); } cpds.setAcquireIncrement(100); //当连接池中的连接用完时,C3P0一次性创建新连接的数目 cpds.setAcquireRetryAttempts(100); //定义在从数据库获取新连接失败后重复尝试获取的次数,默认为30 cpds.setAcquireRetryDelay(1000); //两次连接中间隔时间,单位毫秒,默认为1000 cpds.setAutoCommitOnClose(false); //连接关闭时默认将所有未提交的操作回滚。默认为false cpds.setBreakAfterAcquireFailure(false);//获取连接失败将会引起所有等待获取连接的线程抛出异常。但是数据源仍有效保留,并在下次调 //用getConnection()的时候继续尝试获取连接。如果设为true, //那么在尝试获取连接失败后该数据源将申明已断开并永久关闭。默认为 false cpds.setCheckoutTimeout(0);//当连接池用完时客户端调用getConnection()后等待获取新连接的时间,超时后将抛出SQLException,如设为0则无限期等待。单位毫秒,默认为0; cpds.setMaxIdleTime(10000);//最大空闲时间,超过空闲时间的连接将被丢弃。为0或负数则永不丢弃。默认为0 cpds.setMinPoolSize(100);//连接池中保留的最小连接数 cpds.setMaxPoolSize(1000);//接池中保留的最大连接数。默认为15 cpds.setInitialPoolSize(100);//始化时创建的连接数,应在minPoolSize与maxPoolSize之间取值。默认为3 cpds.setMaxStatements(0);// JDBC的标准参数,用以控制数据源内加载的PreparedStatement数量。但由于预缓存的Statement属 // 于单个Connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素,如果maxStatements与 // maxStatementsPerConnection均为0,则缓存被关闭。默认为0 cpds.setMaxStatementsPerConnection(30);//连接池内单个连接所拥有的最大缓存Statement数。默认为0; cpds.setNumHelperThreads(200);//C3P0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能,通过多线程实现多个操作同时被执行。默认为3 cpds.setPropertyCycle(600);//用户修改系统配置参数执行前最多等待的秒数。默认为300 cpds.setJdbcUrl("*************");//数据库地址 cpds.setUser("****");//用户名 cpds.setPassword("*****");//密码 } private DBconn(){} public static DBconn getInstance(){ return instance; } @Override public Connection getConnection() throws SQLException { try { return cpds.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } @Override public Connection getConnection(String arg0, String arg1) throws SQLException { // TODO Auto-generated method stub return null; } @Override public PrintWriter getLogWriter() throws SQLException { // TODO Auto-generated method stub return null; } @Override public int getLoginTimeout() throws SQLException { // TODO Auto-generated method stub return 0; } @Override public void setLogWriter(PrintWriter arg0) throws SQLException { // TODO Auto-generated method stub } @Override public void setLoginTimeout(int arg0) throws SQLException { // TODO Auto-generated method stub } }
配置文件
<bean id="dataSource" class="DBconn" > </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="packagesToScan" value="model.*" /> </bean>
0 0
- **spring+hibernate + c3p0 代码整合 **
- hibernate、spring、c3p0整合
- hibernate、spring、c3p0整合(连接池)
- Spring整合C3P0和quartz和hibernate
- struts2 hibernate spring c3p0整合 之集成spring
- SSH整合学习笔记之struts2+spring+hibernate+c3p0
- Spring+hibernate 简单整合代码
- hibernate整合C3P0连接池
- SSH整合学习笔记之spring与hibernate整合(二)--C3P0
- SSH框架整合(整合Spring,Hibernate,Struts,c3p0,log4j,mysql)
- spring与Hibernate整合代码截图
- spring+hibernate+c3p0整合链接池配置-连接无法释放问题(已经解决)
- spring+hibernate+c3p0整合链接池配置-连接无法释放问题(已经解决)
- spring+hibernate+c3p0整合链接池配置-连接无法释放问题(已经解决)
- Spring 整合HIbernate 时, 使用c3p0 数据源 出现 java.lang.NoClassDefFoundError:com.mchange.v2.ser.Indirector
- Spring 结合 Hibernate 配置 C3P0
- Spring 结合 Hibernate 配置 C3P0
- Spring 结合 Hibernate 配置 C3P0
- java工具类——二维码
- linux命令之ifup
- Thinking In Java学习进程---对象的入门1.2 对象的接口
- 正则表达式 gim
- 整数划分问题(动态规划)
- **spring+hibernate + c3p0 代码整合 **
- Python:list
- elasticsearch开发
- 在Android Studio中设置编码格式
- JobScheduler学习
- IOS简单的画线功能实现
- devise安装使用指南
- OpenWRT的包依赖 package DEPEND
- linux命令之ifdown