mybatis的连接怎么手动关闭

liuting890925 liuting890925 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
用JDBC连接SQLite:
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager
.getConnection("jdbc:sqlite:/d:/sqlite.db");
Statement stat = conn.createStatement();
ResultSet r =stat.executeQuery("select * from student");
while(r.next())
{
System.out.println(r.getString("name"));
}
conn.close(); // 结束数据库的连接
FileUtils.forceDelete(new File("D:/sqlite.db"));
连接关闭,文件成功删除掉

用mybatis连接SQlite:

SqlSessionFactory factory = connectSQLite();
SqlSession session = factory.openSession();
List<Student> list = session.selectList("SQLiteTest.queryAllStudent");
session.getConnection().close();
FileUtils.forceDelete(new File("D:/sqlite.db"));

private static SqlSessionFactory connectSQLite() throws IOException
{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
Properties proper = createSQLiteProperties();
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader,proper);
reader.close();
return factory;
}

private static Properties createSQLiteProperties()
{
Properties proper = new Properties();
proper.put("driver", "org.sqlite.JDBC");
proper.put("username", "");
proper.put("password", "");
proper.put("url", "jdbc:sqlite:/D:/sqlite.db");
return proper;
}

SqlMapConfig.xml
<configuration>
<environments default="Simple">
<environment id="Simple">
<transactionManager type="JDBC">
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="url" value="${url}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="sqlMap/queryStudent.xml"></mapper>
</mappers>
</configuration>

java.io.IOException: Unable to delete file: D:\sqlite.db
我想问为什么删不掉?mybatis封装的JDBC,用的Connection连接的,Connection关闭,按理说是不应该有进程在占有这个文件的啊,JDBC关闭就可以直接删掉?mybatis关数据库连接的时候都需要关什么?不能手动关闭连接吗?

...全文
评论
收藏 已收藏
分享
返回顶部
liuting890925
Web 开发 81,108 成员 341,721 内容