Mercurial Hosting > luan
comparison src/luan/modules/sql/Database.java @ 1268:725e52076f03
remove broken pooling
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 12 Nov 2018 03:54:04 -0700 |
parents | 9fa8b8389578 |
children | 25746915a241 |
comparison
equal
deleted
inserted
replaced
1267:9fa8b8389578 | 1268:725e52076f03 |
---|---|
1 package luan.modules.sql; | 1 package luan.modules.sql; |
2 | 2 |
3 import java.io.Closeable; | |
4 import java.sql.Connection; | 3 import java.sql.Connection; |
5 import java.sql.DriverManager; | 4 import java.sql.DriverManager; |
6 import java.sql.Statement; | 5 import java.sql.Statement; |
7 import java.sql.PreparedStatement; | 6 import java.sql.PreparedStatement; |
8 import java.sql.ResultSet; | 7 import java.sql.ResultSet; |
15 import luan.LuanState; | 14 import luan.LuanState; |
16 import luan.LuanTable; | 15 import luan.LuanTable; |
17 import luan.LuanException; | 16 import luan.LuanException; |
18 | 17 |
19 | 18 |
20 public final class Database implements Closeable { | 19 public final class Database { |
21 private static final Logger logger = LoggerFactory.getLogger(Database.class); | 20 private static final Logger logger = LoggerFactory.getLogger(Database.class); |
22 | 21 |
23 private static Map<Map,Database> pool = new HashMap<Map,Database>(); | |
24 | |
25 public static Database get(Connection con) { | |
26 return new Database(con); | |
27 } | |
28 | |
29 public static synchronized Database get(LuanTable specTbl) | |
30 throws LuanException, ClassNotFoundException, SQLException | |
31 { | |
32 Map<Object,Object> spec = specTbl.asMap(); | |
33 Database db = pool.get(spec); | |
34 if( db==null ) { | |
35 db = new Database(spec); | |
36 pool.put(spec,db); | |
37 } | |
38 return db; | |
39 } | |
40 | |
41 public int uses = 0; | |
42 public final Connection con; | 22 public final Connection con; |
43 private final Map<String,PreparedStatement> pstmts = new HashMap<String,PreparedStatement>(); | 23 private final Map<String,PreparedStatement> pstmts = new HashMap<String,PreparedStatement>(); |
44 | 24 |
45 public Database(Connection con) { | 25 public Database(Connection con) { |
46 this.con = con; | 26 this.con = con; |
47 } | 27 } |
48 | 28 |
49 private Database(Map<Object,Object> spec) | 29 public Database(LuanTable specTbl) |
50 throws LuanException, ClassNotFoundException, SQLException | 30 throws LuanException, ClassNotFoundException, SQLException |
51 { | 31 { |
52 spec = new HashMap<Object,Object>(spec); | 32 Map<Object,Object> spec = specTbl.asMap(); |
53 String cls = getString(spec,"class"); | 33 String cls = getString(spec,"class"); |
54 Class.forName(cls); | 34 Class.forName(cls); |
55 String url = getString(spec,"url"); | 35 String url = getString(spec,"url"); |
56 Properties props = new Properties(); | 36 Properties props = new Properties(); |
57 props.putAll(spec); | 37 props.putAll(spec); |
99 PreparedStatement pstmt = prepareStatement(sql,args); | 79 PreparedStatement pstmt = prepareStatement(sql,args); |
100 return pstmt.executeUpdate(); | 80 return pstmt.executeUpdate(); |
101 } | 81 } |
102 } | 82 } |
103 | 83 |
104 public void close() { | |
105 try { | |
106 con.close(); | |
107 } catch(SQLException e) { | |
108 throw new RuntimeException(e); | |
109 } | |
110 } | |
111 | |
112 protected void finalize() throws Throwable { | |
113 if( !con.isClosed() ) { | |
114 logger.error("not closed"); | |
115 close(); | |
116 } | |
117 super.finalize(); | |
118 } | |
119 | |
120 } | 84 } |