项目重构之数据源配置与优化:log4j 配置数据库连接池Druid,并
作者 :泥沙砖瓦浆木匠 网站 : http://blog.csdn.net/jeffli1993 个人签名 : 打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节。 交流QQ群 :【编程之美 365234583】http://qm.qq.com/cgi-bin/qm/qr?k=FhFAoaWwjP29_AonqzL0rpdQAjjqlHQQ 如果我的
作者:泥沙砖瓦浆木匠
网站:http://blog.csdn.net/jeffli1993
个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节。
交流QQ群:【编程之美 365234583】http://qm.qq.com/cgi-bin/qm/qr?k=FhFAoaWwjP29_AonqzL0rpdQAjjqlHQQ
如果我的帮到了你,是否乐意捐助一下或请一杯啤酒也好呢?有你支持,干的更好~
点这参与众筹 我的支付宝:13958686678
一、 前言
泥瓦匠又和大家见面了,最近两天我在Code Review ,顺便代码小小的Refactoring(重构)下。先了解这个项目吧,这次解决的是数据源配置优化。因为这web项目中配置数据源的地方很多。例如JDBC要配置数据源,Mybatis要配置数据源,Quartz定时任务要配置数据源,还有Log4j存记录到数据库也要配置…
如题目,兴许大家的疑惑看了前面的说明会明白。这次给大家带来的 数据源配置与优化:log4j 配置数据库连接池Druid。
提纲:
- 二、准备知识
- 三、正文 开始动手吧
- 、配置文件
- 、建数据表
- 、核心代码讲解
- 四、结论及下载
二、准备知识
泥瓦匠也是怕自己说不清楚,又不想把Log4j 和 Durid介绍个遍。那样会太麻烦。这次主要是项目实战。所以泥瓦匠也不罗嗦知识准备这块也就是点到为止。
Log4j 简介
在应用程序中添加日志记录总的来说基于三个目的:
监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作;
跟踪代码运行时轨迹,作为日后审计的依据;
担当集成开发环境中的调试器的作用,向文件或控制台打印代码的调试信息。
它支持将日志信息输入到数据库,这次我们一Mysql为例说明。我们需要Log4j来将调试信息、操作信息等记录下来,以便后面的审计,这些日志信息包括用户ID、用户姓名、操作类、路径、方法、操作时间、日志信息。
Druid 简介
Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。不多多介绍了,阿里牛人作品必须精品。详细介绍在:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
泥瓦匠的任务很简单,目的是为了数据源配置优化,实现数据源配置的唯一性。泥瓦匠也画了草图,曾经被人说成画图画出鬼画符的我告诉大家:没事,越做越好。如图,设计简单草图就是这样的。
结论:一个项目的配置文件要保持唯一性,就是数据源配置的唯一性。
三、正文 开始动手吧
开始弄吧,为了写这个东西。我就也搞了个demo项目。泥瓦匠很辛苦的,哈哈送钱的上面支付宝哦。哈哈,泡了杯水,准备说这个项目了。下面,泥瓦匠先给出这个项目的结构图,这样我待会讲起来不怎么累。逻辑性比较强吧。如图:
上面很清楚的写着我需要完成的功能模块。最后那个test,是一个测试的servlet类。大家一看就明白。我先从配置文件说起吧。
配置文件 :
dbConfig.properties 记录的是最基础的db配置:url name psd 等,代码如下:
1 2 3 4 5 6 7 8 |
|
log4j.properties则是日志的配置,但日志的配置中有一点注意的是如下:
1 2 3 4 5 6 7 8 9 10 |
|
在这里,我们要注意两点:
一、设置我们包的下的logger权限,并给予存数据库的权限。
二、db的实现的应用要为你写的引用类。
1 2 |
|
建数据表 :
然后我们要根据上面配置文件中写的数据库和表格我们要对应的建一个名为test的数据库和一张名为operate_log的表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
这样我们就可以开始写代码了,泥瓦匠是一个很细节的人。这次博客,我主要是讲下代码中的核心点。因为后面我会把项目提供给你们下载。所以,这里点到为止。
泥瓦匠说:点到为止,所谓师父领进门,修行在个人啊。
核心代码讲解:
MyJDBCAppender.java 用于Log4j的数据库Session管理[连接池用Druid]。这个肯定是我们得核心思想。这里我就继承了log4j提供的org.apache.log4j.jdbc.JDBCAppender;然后只要简单的重写了closeConnection和getConnection方法。如果获取Druid数据库源对象异常的话,我还写了个 取消初始化 的方法uninitialize。代码这边也贴出下,方便大家观看:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
值得注意的一点是,泥瓦匠为了方便,所以在其中的地方没有获取druid连接池的配置。而是直接写了下面:
1 2 3 |
|
其实这样写是不好了,我们可以写一个druid.properties然后将连接池的配置放入其中。获取set,for循环set即可。这边我就不实现了。很简单哦,泥瓦匠相信你们。
最后我演示下,示例代码:放到tomcat7上,然后运行访问
看到控制台刷出来两条信息,因为我门设置的是log4j.logger.test=INFO, db。
然后我们去查看数据库,这边我用Navicat:
四、结论及下载
结论:重构很有意思,慢慢来,一点一点来,就行了。细节成就未来。
下载链接:
http://pan.baidu.com/s/1hqKN0Le
如以上文章或链接对你有帮助的话,别忘了在文章按钮或到页面右下角点击 “赞一个” 按钮哦。你也可以点击页面右边“分享”悬浮按钮哦,让更多的人阅读这篇文章

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
