数据源和连接池

发布时间:2024-12-15 点击:148
数据源
所谓数据源也就是数据的来源,它存储了所有建立数据库连接需要的信息。就像通过指定文件名称可以在文件系统中找到文件一样,通过提供正确的数据源名称,你可以找到相应的数据库连接。
因此,一个数据源只对应于一个数据库,如果项目中需要连多个数据库,那么就要配置多个数据源。
数据源xml配置实例:
<!--数据源--> <bean id=datasource class=com.zaxxer.hikari.hikaridatasource destroy-method=close> <constructor-arg> <bean class=com.zaxxer.hikari.hikariconfig> <property name=poolname value=hikaridatasource /> <property name=driverclassname value=${jdbc.driverclassname} /> <property name=jdbcurl value=${jdbc.url} /> <property name=username value=${jdbc.username} /> <property name=password value=${jdbc.password} /> <property name=autocommit value=false /> <property name=connectiontimeout value=30000 /> <property name=idletimeout value=600000 /> <property name=maxlifetime value=1800000 /> <property name=connectiontestquery value=${jdbc.testsql} /> <property name=maximumpoolsize value=500 /> <property name=minimumidle value=1 /> </bean> </constructor-arg> </bean> <!--jdbctemplate需要引用数据源--> <bean id=jdbctemplate class=org.springframework.jdbc.core.jdbctemplate> <property name=datasource ref=datasource></property> </bean> <!--事务也是加在数据源之上的--> <bean id=transactionmanager class=org.springframework.jdbc.datasource.datasourcetransactionmanager> <property name=datasource ref=datasource></property> </bean> <tx:advice id=transactionadvice transaction-manager=transactionmanager> <tx:attributes> <tx:method name=* propagation=required /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression=execution(* com.btzh.service.impl.*.*(..)) id=allmanagermethod /> <aop:advisor advice-ref=transactionadvice pointcut-ref=allmanagermethod /> </aop:config>可见,jdbc模板、事务都是以数据源为基础,所有配置都是围绕着数据源开展的。
再看springboot的数据源配置:
#-------------------------------------------------- 数据库配置 --------------------------------------------------#spring.datasource.ms.driver-class-name=com.mysql.jdbc.driverspring.datasource.ms.jdbcurl=jdbc:mysql://192.168.47.130:3306/faith?usessl=false&servertimezone=cttspring.datasource.ms.username=faithspring.datasource.ms.password=faith# 连接池的配置信息,下面为连接池的补充设置,应用到上面所有数据源中# 初始化大小,最小,最大spring.datasource.initialsize=5spring.datasource.minidle=5spring.datasource.maxactive=20# 配置获取连接等待超时的时间spring.datasource.maxwait=60000# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒spring.datasource.timebetweenevictionrunsmillis=60000# 配置一个连接在池中最小生存的时间,单位是毫秒spring.datasource.minevictableidletimemillis=300000spring.datasource.validationquery=select 1 from dualspring.datasource.testwhileidle=truespring.datasource.testonborrow=falsespring.datasource.testonreturn=false# 打开pscache,并且指定每个连接上pscache的大小spring.datasource.poolpreparedstatements=truespring.datasource.maxpoolpreparedstatementperconnectionsize=20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,\\\'wall\\\'用于防火墙spring.datasource.filters=stat,wall,log4jspring.datasource.logslowsql=true# 通过connectproperties属性来打开mergesql功能;慢sql记录spring.datasource.connectionproperties=druid.stat.mergesql=true;druid.stat.slowsqlmillis=5000/ * 阿里druid连接池配置方式 */@slf4j@configurationpublic class druiddbconfig { @value(${spring.datasource.ms.driver-class-name}) private string driverclassname; @value(${spring.datasource.ms.jdbcurl}) private string dburl; @value(${spring.datasource.ms.username}) private string username; @value(${spring.datasource.ms.password}) private string password; @value(${spring.datasource.initialsize}) private int initialsize; @value(${spring.datasource.minidle}) private int minidle; @value(${spring.datasource.

苹果iPhone 12再曝光,总算找到一个贴心的设计了,超赞
租云服务器干什么的
工信部发布《通信短信息和语音呼叫服务管理规定(征求意见稿)》
掌上云服务器怎么安装驱动软件
阿里云服务器学生专享哪个好
IDC:2024年全球边缘计算市场将达到2506亿美元
跻身国内混合云市场第一 这家运营商如何做到
【EXPDP】使用EXPDP备份数据时预估大小——ESTIMATE参数