我正在使用池化数据源(msaccess数据库)通过我制作的应用程序更新本地数据库(使用h2数据库的客户端)。
我遇到的问题是,在提交请求时说“向用户(名称,代码)插入值(Me,hfd5255fd4
);”该应用程序运行正常,错误日志中未报告任何内容,但数据库中也未更改。
我正在使用的代码如下
private static Connection getDatabase() throws Exception {
cpds.setDriverClass("net.ucanaccess.jdbc.UcanaccessDriver");
// loads the jdbc driver
cpds.setJdbcUrl("jdbc:ucanaccess://"
+ (new File("Ressources/filter.mdb").getAbsolutePath()));
cpds.setUser("admin");
cpds.setPassword("ibnsina");
cpds.setAutoCommitOnClose(false);
return cpds.getConnection(); //tried removing this , but no effect
}
----doing some other stuff---
private static updating() throws exception{
conn = getDatabase();
File fileUpload = new File(logPath + "UploadLog.txt");
BufferedReader readerUpload = new BufferedReader(new FileReader(
fileUpload));
String Uploadingline = "";
StringBuffer secondaryline = new StringBuffer();
if (readerUpload.ready()) {
System.out.println("Uploadtxt ready");
Statement stUpload = conn.createStatement();
System.out.println("Stupload ready");
while ((Uploadingline = readerUpload.readLine()) != null) {
if (Uploadingline.endsWith(";")) {
secondaryline.append(Uploadingline);
/*stUpload.executeUpdate(secondaryline.toString()); tried this to execute each line separatly*/
stUpload.addBatch(secondaryline.toString());
System.out.println("Reading line :" + secondaryline);
secondaryline.setLength(0);
} else {
secondaryline.append(Uploadingline);
}
}
stUpload.executeBatch();
stUpload.clearBatch();
conn.commit(); //i even tried adding this to make it commit even tho autocommit is by default ON
stUpload.close();}
最佳答案
您不应该为每个连接创建一个新的DataSource
,只需创建一个DataSource
并使用它来获取Connection
。记住要close()
它们,因为那样会将连接返回到池中。
您应该执行以下操作:
// There should only ever be one of these.
private static final DataSource ds = makeDataSource();
private static DataSource makeDataSource() {
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("net.ucanaccess.jdbc.UcanaccessDriver");
// loads the jdbc driver
cpds.setJdbcUrl("jdbc:ucanaccess://"
+ (new File("Ressources/filter.mdb").getAbsolutePath()));
cpds.setUser("admin");
cpds.setPassword("ibnsina");
cpds.setAutoCommitOnClose(false);
return cpds;
}
private static Connection getConnection () {
return ds.getConnection();
}
private static void releaseConnection (Connection conn) {
conn.commit();
conn.close();
}
private static void updating() {
Connection conn = getConnection();
try {
//...
} finally {
releaseConnection(conn);
}
}
关于java - c3p0 CombopooledDataSource没有提交SQL更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27036998/