我在Spring+MyBatis项目中想做一个独立的类,手动获取到数据库连接,用于操作数据库,单例的,不想每次使用都创建连接,而是一个不关闭的长连接。但是现在问题来了,单例实现了。SqlSession 也一直都在,Debug下SqlSession的Id也没有变化。应该是达到要求了,但是通过SqlSession.OpenConnection()获得到的数据库连接,在第一次调用的时候还有,第二次就抛异常了。显示此连接为空。我跟过MyBatis的源码,每次执行结束会Close掉SqlSession,但是我这个单例应该是不受MyBatis影响的,只是借用了它的sessionFactory。百思不得其解。跪求大神解惑呀,上代码:class DataBaseHelper {
// Spring配置文件路径,相对于classpath
final String Spring_conf_path = "applicationContext.xml";
// jdbc配置文件路径,相对于classpath
final String jdbc_conf_path = "fiad.properties";
// SessionFactory在Spring中的配置ID
final String factory_id = "sqlSessionFactory";
//私有,单例避免重复创建
private SqlSession session;
private Connection connection;
//单例实现
private static class DataBaseHolder {
private static final DataBaseHelper INSTANCE = new DataBaseHelper();
} public static final DataBaseHelper getInstance() {
return DataBaseHolder.INSTANCE;
} private DataBaseHelper() {
initConnection();
} private void initConnection() {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource(Spring_conf_path));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new ClassPathResource(jdbc_conf_path));
cfg.postProcessBeanFactory(factory);
SqlSessionFactory sessionFactory = (SqlSessionFactory) factory
.getBean(factory_id);
session = sessionFactory.openSession(true);
connection = session.getConnection();
}
/**
* 插入系统日志
*
* @param log
* @return
* @throws Exception
*/
int insertLogs(LogBean log) throws Exception {
int i = 0;
PreparedStatement st = null;
try {
//此处第1次调用毫无问题,但当第二次调用的时候,就抛空指针异常了。
st = connection.prepareStatement(log.getInsert());
// dataContent,createTime,userID,moduleID,featureID
st.setString(1, log.getDataContent());
st.setTimestamp(2, log.getCreateTime());
st.setInt(3, log.getUserID());
st.setInt(4, log.getModuleID());
st.setInt(5, log.getFeatureID());
i = st.executeUpdate();
} catch (Exception ex) {
throw ex;
} finally {
if (st != null)
st.close();
}
return i;
}
}
// Spring配置文件路径,相对于classpath
final String Spring_conf_path = "applicationContext.xml";
// jdbc配置文件路径,相对于classpath
final String jdbc_conf_path = "fiad.properties";
// SessionFactory在Spring中的配置ID
final String factory_id = "sqlSessionFactory";
//私有,单例避免重复创建
private SqlSession session;
private Connection connection;
//单例实现
private static class DataBaseHolder {
private static final DataBaseHelper INSTANCE = new DataBaseHelper();
} public static final DataBaseHelper getInstance() {
return DataBaseHolder.INSTANCE;
} private DataBaseHelper() {
initConnection();
} private void initConnection() {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource(Spring_conf_path));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new ClassPathResource(jdbc_conf_path));
cfg.postProcessBeanFactory(factory);
SqlSessionFactory sessionFactory = (SqlSessionFactory) factory
.getBean(factory_id);
session = sessionFactory.openSession(true);
connection = session.getConnection();
}
/**
* 插入系统日志
*
* @param log
* @return
* @throws Exception
*/
int insertLogs(LogBean log) throws Exception {
int i = 0;
PreparedStatement st = null;
try {
//此处第1次调用毫无问题,但当第二次调用的时候,就抛空指针异常了。
st = connection.prepareStatement(log.getInsert());
// dataContent,createTime,userID,moduleID,featureID
st.setString(1, log.getDataContent());
st.setTimestamp(2, log.getCreateTime());
st.setInt(3, log.getUserID());
st.setInt(4, log.getModuleID());
st.setInt(5, log.getFeatureID());
i = st.executeUpdate();
} catch (Exception ex) {
throw ex;
} finally {
if (st != null)
st.close();
}
return i;
}
}
解决方案 »
- Hibernate中的criteria不能按月份进行排序吗?
- java 系统时间 -- 若用户修改电脑系统时间,能否得到准确时间
- struts+hibernate上传图片到数据库的问题!!!
- 部署中出现的怪现象
- weblogic81 建立连接池问题:,总是报错:JDBC driver is not on the CLASSPATH
- Jfreechart调试不通,
- 用JCombobox时碰到的奇怪问题,急!!!
- 关于EJB的几个概念比较模糊,请问????????????
- Spring声明式事务问题(虚心请教)
- 阿里监控和调优性能工具tprofiler 报错,不能监控springboot2.0,但是springboot1.5.3可以
- hibernate与oracle12c连接的问题
- Hibernate 多对多双向关联 中间表 无法插入数据,求大神,在线等。用的S2HS框架
BasicDataSource dataSource = (BasicDataSource) factory
.getBean(dataSourceId);
String dbUsername=dataSource.getUsername();
String dbDriver = dataSource.getDriverClassName();
String dbPassword = dataSource.getPassword();
String dbUrl = dataSource.getUrl();
try {
Class.forName(dbDriver);
connection = DriverManager.getConnection(dbUrl,dbUsername,dbPassword);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}