我使用 Tomcat 6.0、mybatis 和 Oracle XAPool 开发应用程序。当我运行应用程序时,会生成以下堆栈跟踪。
2012-05-10 16:19:16,328 ERROR [jatis.avantrade.foundation.model.dao.DAOHelper] [t] - datasource jndi : org.enhydra.jdbc.pool.StandardXAPoolDataSource
2012-05-10 16:19:16,328 ERROR [jatis.avantrade.foundation.model.dao.DAOHelper] [t] - org.enhydra.jdbc.pool.StandardXAPoolDataSource cannot be cast to org.enhydra.jdbc.pool.StandardXAPoolDataSource
java.lang.ClassCastException: org.enhydra.jdbc.pool.StandardXAPoolDataSource cannot be cast to org.enhydra.jdbc.pool.StandardXAPoolDataSource
at jatis.avantrade.foundation.model.dao.DAOHelper.getSession(DAOHelper.java:73)
下面是获取数据库会话时的 DAOHelper 代码。当我打印数据源时,它显示它的类型StandardXAPoolDataSource
。奇怪的行为是堆栈跟踪显示org.enhydra.jdbc.pool.StandardXAPoolDataSource
不能强制转换为org.enhydra.jdbc.pool.StandardXAPoolDataSource
.
同一个类不能投射到自己身上吗?
protected SqlSession getSession() {SqlSession session = sessionLocal.get();
if (session == null) {
ConfigurationManager configurationManager = ConfigurationManagerSupport .getConfigurationManager(5000);
RootContextConfiguration contextConfiguration = (RootContextConfiguration) configurationManager
.getContextConfiguration(RootContextConfiguration.CONTEXT);
session = contextConfiguration.getMyBatisSession(MYBATIS_ID);
log.error("data source : "
+ session.getConfiguration().getEnvironment()
.getDataSource());
log.error("connection : " + session.getConnection());
sessionLocal.set(session);
log.error("factory : "
+ session.getConfiguration().getEnvironment()
.getTransactionFactory());
String DATASOURCE_CONTEXT = "java:comp/env/jdbc/avantrade";
Connection result = null;
try {
Context initialContext = new InitialContext();
DataSource datasource = (DataSource) initialContext
.lookup(DATASOURCE_CONTEXT);
log.error("datasource jndi : " + datasource);
log.error("datasource jndi : " + datasource.getClass().getName());
log.error("datasource jndi name : " + ((StandardXAPoolDataSource) datasource).getDataSourceName());
log.error("datasource jndi connection : " + ((StandardXAPoolDataSource) datasource).getConnection());
{
result = datasource.getConnection();
log.error("connection jndi : " + result);
log.error("connection jndi : " + result.getClass().getName());
}
} catch (NamingException ex) {
log.error(ex.getMessage(), ex);
} catch (SQLException ex) {
log.error(ex.getMessage(), ex);
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
}
}
return session;
}
谢谢。