- 浏览: 1512200 次
- 性别:
- 来自: 厦门
-
文章分类
- 全部博客 (516)
- Java (49)
- Java/Struts 2.0 (25)
- Java/Spring、Spring MVC (11)
- Java/Quartz (3)
- Java/Lucene (6)
- Java/Hibernate (19)
- Java/openJPA (7)
- Java/DWR (7)
- Java/Security、Spring Security/OAuth2 (6)
- Java/Threading (9)
- Java/XML (22)
- java/design pattern (4)
- Android (2)
- JavaScript (46)
- jquery (3)
- DB/MySQL (23)
- DB/Oracle (16)
- PHP (25)
- CSS (20)
- Linux (38)
- C/C++、DLL、Makefile、VC++ (31)
- 正则 (9)
- Eclipse (4)
- 安全、网络等概念 (25)
- 集群 (7)
- 网页 (5)
- 视频\音频 (1)
- HTML (6)
- 计算机数学/算法 (3)
- Virtualbox (1)
- LDAP (2)
- 数据挖掘 (6)
- 工具破解 (1)
- 其他 (13)
- Mail (1)
- 药材 (3)
- 游戏 (2)
- hadoop (13)
- 压力测试 (3)
- 设计模式 (3)
- java/Swing (2)
- 缓存/Memcache (0)
- 缓存/Redis (1)
- OSGI (2)
- OSGI/Gemini (0)
- 文档写作 (0)
- java/Servlet (3)
- MQ/RabbitMQ (2)
- MQ/RocketMQ (0)
- MQ/Kafka (1)
- maven (0)
- SYS/linux (1)
- cache/redis (1)
- DB/Mongodb (2)
- nginx (1)
- postman (1)
- 操作系统/ubuntu (1)
- golang (1)
- dubbo (1)
- 技术管理岗位 (0)
- mybatis-plus (0)
最新评论
-
pgx89112:
大神,请赐我一份这个示例的项目代码吧,万分感谢,1530259 ...
spring的rabbitmq配置 -
string2020:
不使用增强器 怎么弄?
OpenJPA的增强器 -
孟江波:
学习了,楼主,能否提供一份源代码啊,学习一下,十分感谢!!!4 ...
spring的rabbitmq配置 -
eachgray:
...
spring-data-redis配置事务 -
qljoeli:
学习了,楼主,能否提供一份源代码啊,学习一下,十分感谢!!!1 ...
spring的rabbitmq配置
d
转自:来源忘记了。
/** * JavaGGDataSource.java 2011-3-4 上午09:21:05 */ package test.datasource; import java.io.PrintWriter; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Connection; import java.sql.Driver; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.ReentrantLock; import javax.sql.DataSource; /** * @author linzq * */ public class JavaGGDataSource implements DataSource { // 连接队列 private ConcurrentLinkedQueue<_Connection> connQueue = new ConcurrentLinkedQueue<_Connection>(); // 存放所有连接容器 private List<_Connection> conns = new ArrayList<_Connection>(); private Driver driver = null; private String jdbcUrl = null; private String user = null; private String password = null; // -1为不限制连接数 private int maxActive = -1; private String driverClass = null; // 默认为4小时,即4小时没有任何sql操作就把所有连接重新建立连接 private int timeout = 1000 * 60 * 60 * 4; private AtomicLong lastCheckout = new AtomicLong( System .currentTimeMillis()); private AtomicInteger connCount = new AtomicInteger(); // 线程锁,主要用于新建连接和清空连接时 private ReentrantLock lock = new ReentrantLock(); public void closeAllConnection() { } /** * * 归还连接给连接池 * * * * @param conn * *@date 2009-8-13 * *@author eric.chan */ public void offerConnection(_Connection conn) { connQueue.offer(conn); } @Override public Connection getConnection() throws SQLException { return getConnection(user, password); } /** * * 从池中得到连接,如果池中没有连接,则建立新的sql连接 * * * * @param username * * @param password * * @author eric.chan */ @Override public Connection getConnection(String username, String password) throws SQLException { checkTimeout(); _Connection conn = connQueue.poll(); if (conn == null) { if (maxActive > 0 && connCount.get() >= maxActive) { for (;;) { // 采用自旋方法 从已满的池中得到一个连接 conn = connQueue.poll(); if (conn != null) break; else continue; } } lock.lock(); try { if (maxActive > 0 && connCount.get() >= maxActive) { // 处理并发问题 return getConnection(username, password); } Properties info = new Properties(); info.put("user", username); info.put("password", password); Connection conn1 = loadDriver().connect(jdbcUrl, info); conn = new _Connection(conn1, this); int c = connCount.incrementAndGet();// 当前连接数加1 conns.add(conn); System.out.println("info : init no. " + c + " connectioned"); } finally { lock.unlock(); } } lastCheckout.getAndSet(System.currentTimeMillis()); return conn.getConnection(); } /** * * 检查最后一次的连接时间 * * * * @throws SQLException * *@date 2009-8-13 * *@author eric.chan */ private void checkTimeout() throws SQLException { long now = System.currentTimeMillis(); long lt = lastCheckout.get(); if ((now - lt) > timeout) { _Connection conn = null; lock.lock(); try { if (connCount.get() == 0) return; while ((conn = connQueue.poll()) != null) { System.out.println("connection " + conn + " close "); conn.close(); conn = null; } for (_Connection con : conns) { con.close(); } conns.clear(); System.out.println("info : reset all connections"); // 重置连接数计数器 connCount.getAndSet(0); lastCheckout.getAndSet(System.currentTimeMillis()); } finally { lock.unlock(); } } } /** * * * * @return * *@date 2009-8-13 * *@author eric.chan */ private Driver loadDriver() { if (driver == null) { try { driver = (Driver) Class.forName(driverClass).newInstance(); } catch (ClassNotFoundException e) { System.out.println("error : can not find driver class " + driverClass); } catch (Exception e) { e.printStackTrace(); } } return driver; } @Override public PrintWriter getLogWriter() throws SQLException { return null; } @Override public int getLoginTimeout() throws SQLException { return 0; } @Override public void setLogWriter(PrintWriter out) throws SQLException { } @Override public void setLoginTimeout(int seconds) throws SQLException { } @Override public boolean isWrapperFor(Class iface) throws SQLException { throw new SQLException("no Implemented isWrapperFor method"); } @Override public <T> T unwrap(Class<T> iface) throws SQLException { throw new SQLException("no Implemented unwrap method"); } public String getJdbcUrl() { return jdbcUrl; } public void setJdbcUrl(String jdbcUrl) { this.jdbcUrl = jdbcUrl; } public String getUsername() { return user; } public void setUsername(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDriverClass() { return driverClass; } public void setDriverClass(String driverClass) { this.driverClass = driverClass; } public int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout * 1000; } public void setMaxActive(int maxActive) { this.maxActive = maxActive; } public int getMaxActive() { return maxActive; } } /** * 数据连接的自封装 ,是java.sql.Connection的一个钩子,主要是处理close方法 * * @author linzq * */ class _Connection implements InvocationHandler { private final static String CLOSE_METHOD_NAME = "close"; private final Connection conn; private final JavaGGDataSource ds; _Connection(Connection conn, JavaGGDataSource ds) { this.conn = conn; this.ds = ds; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object obj = null; // 判断是否调用了close的方法,如果调用close方法则把连接置为无用状态 if (CLOSE_METHOD_NAME.equals(method.getName())) { // 归还连接给连接池 ds.offerConnection(this); } else { // 运行非close的方法 obj = method.invoke(conn, args); } return obj; } public Connection getConnection() { // 返回数据库连接conn的接管类,以便截住close方法 Connection conn2 = (Connection) Proxy.newProxyInstance(conn.getClass() .getClassLoader(), new Class[] { Connection.class }, this); return conn2; } public void close() throws SQLException { // 调用真正的close方法,一但调用此方法就直接关闭连接 if (conn != null && !conn.isClosed()) conn.close(); } }
/** * TestGG.java 2011-3-4 上午10:02:14 */ package test.datasource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * @author linzq * */ public class TestGG { /** * @param args */ public static void main(String[] args) { JavaGGDataSource ds = new JavaGGDataSource(); ds.setDriverClass("com.mysql.jdbc.Driver"); ds.setJdbcUrl("jdbc:mysql://localhost:3306/test"); ds.setUsername("root"); ds.setPassword("123456"); ds.setTimeout(300); // ds.setMaxActive(60); for (int i = 0; i < 20; i++) { new GG(ds).start(); } } static class GG extends Thread { JavaGGDataSource ds = null; long l = System.currentTimeMillis(); public GG(JavaGGDataSource ds) { this.ds = ds; } static final String sql = "insert into testgg(col1,cols) values (?,?)"; static final String selectsql = "select * from testgg where id=?"; public void run() { for (int t = 0; t < 10000; t++) { Connection conn = null; try { conn = ds.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); // 以下为insert ps.setInt(1, 133664); ps.setString(2, "ddd"); ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); // 以下为select // 取得自增长ID ps = conn.prepareStatement(selectsql); if (rs.next()) { // System.out.println(rs); ps.setInt(1, rs.getInt("GENERATED_KEY"));// 表的字段名字也可以用字段下标值 } rs = ps.executeQuery(); while (rs.next()) { rs.getInt("id"); rs.getInt("col1"); } rs.close(); ps.close(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) { // ds.offerConnection(conn); conn.close(); } } catch (Exception e) { e.printStackTrace(); } } } System.out.println(System.currentTimeMillis() - l); } } }
数据库表结构:
CREATE TABLE `testgg` ( `id` int(11) NOT NULL auto_increment, `col1` int(11) default NULL, `cols` varchar(200) default NULL, PRIMARY KEY (`id`) )
d
发表评论
-
Java线程:线程状态的转换
2012-01-30 16:09 900转自:http://lavasoft.blog.51cto.c ... -
多个线程访问共享对象和数据的方式
2011-10-26 08:15 1125(1)如果每个线程执行的代码相同,可以使用同一个Runnabl ... -
线程同步通信
2011-10-22 19:48 843看传智播客张孝祥讲的一个例子。 子线程循环10次 ... -
Java threads may not use all your CPUs
2010-06-10 11:51 1022Enterprise-level applications o ... -
java.util.concurrent.CountDownLatch
2009-12-13 10:53 1860import java.util.concurrent. ... -
java.util.concurrent.Exchanger
2009-12-13 10:52 2894d import java.util.concurrent. ... -
java.util.concurrent.Semaphore
2009-12-11 22:25 2617http://zhidao.baidu.com/questio ... -
java.util.concurrent.CyclicBarrier
2009-12-11 11:46 4683在实际应用中,有时候需要多个线程同时工作以完成同一件事情,而且 ...
相关推荐
DataFrame iloc练习.ipynb
plc
制造企业数字化中台(技术中台、数据中台、业务中台)建设方案.pptx
实验二 预习报告.docx
20240702作业1
浅谈PLC在自动洗车系统中的应用.doc
Lengstorf, Hansen, Prettyman -- PHP 8 for Absolute Beginners -- 2022
智慧校园建设是在国家政策推动下,为深化教育改革、提升教育质量和管理效率而提出的重要项目。该项目旨在通过信息化手段,解决传统教育中存在的资源分散、管理混乱等问题,实现教育资源的高效利用和教学质量的全面提升。 目前,教育信息化虽取得一定进展,但面临“孤岛架构”的挑战,包括硬件资源无法共享、数据孤岛、应用孤岛等问题,导致资源浪费和管理效率低下。为此,智慧校园的建设目标聚焦于家校沟通便捷化、校园管理科学化、校园生活轻松化、课堂教学互动化和校园设施智能化,以提高教学效率和学生学习体验。 智慧校园的核心价值在于构建先进的网络教学平台和管理信息系统,实现教学资源的高效配置和利用,促进师生互动,提高管理效率,降低成本,构建健康高雅的生活环境。解决方案涵盖综合应用平台规划、系统架构设计、媒体发布、数字会议系统等,通过后台服务层、基础接入层和用户接入层的有机结合,实现智慧校园的全面功能。 智慧校园管理平台作为核心组成部分,提供模块化体系,包括公开课、直播、教学资源等23大应用,支持与第三方接口对接,实现多级管理。电教预约管理平台通过移动端APP或web后台简化预约流程,提高教室和会议室资源利用率,支持会议预订、审批、信息发布和环境管控。 教育录播系统和云平台支持教师制作和分享优质教学资源,进行在线组卷和评卷,同时提供学生应用,如高清视频录制、在线直播和互动交流,促进教学资源的共享和教育均衡化发展。这些系统的整合应用,将极大地推动教育信息化进程,实现教育资源的最大化利用和教育质量的全面提升。
c#输出文字5行5列随机组合代码
制造集团企业供应链整体方案含数字化.pptx
皮带运输机PLC电气控制系统设计.doc
Солем -- Программирование компьютерного зрения на языке Python -- 2016
2022-2028全球与中国GaAs器件市场现状及未来发展趋势.docx
Reitz, Schlusser -- The Hitchhiker's Guide to Python -- 2016
当然可以!以下是一些JavaScript语言教程、案例和相关项目资源的推荐: 教程资源: MDN Web 文档:MDN提供了JavaScript的全面教程,包括语法、数据类型、函数、面向对象编程等内容。 JavaScript 教程 - 廖雪峰:廖雪峰的网站提供了简洁明了的JavaScript教程,适合初学者入门。 W3Schools JavaScript 教程:W3Schools是一个广为人知的Web开发教程网站,提供了丰富的JavaScript教程和示例。 案例资源: JavaScript30:由 Wes Bos 创作的免费 JavaScript 30 天挑战,每天一个小项目,涵盖了各种 JavaScript 技术和 API。 CodePen:CodePen是一个在线社区,提供了大量的前端开发案例,你可以在这里找到各种JavaScript项目的示例和代码。 相关项目资源: GitHub:GitHub是一个代码托管平台,你可以在上面找到各种JavaScript项目和库,可以通过搜索关键词来发现你感兴趣的项目。 Awesome JavaScript:这是一个GitHub仓库,收集
pycharm安装教程
数字化转型背景下的企业数据资产管理两份文件.pptx
C#输出2500进制2位数文字排列组合代码正确