gpt4 book ai didi

java - c3p0 CombopooledDataSource 未提交 SQL 更新

转载 作者:行者123 更新时间:2023-12-02 05:14:52 26 4
gpt4 key购买 nike

我正在使用池数据源(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com