- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Hibernate 的 Apache DBCP 连接池错误:初始 SessionFactory 创建失败。不知道有什么问题吗?请参阅第 17 行和第 158 行的注释。
> Initial SessionFactory creation failed.java.lang.NullPointerException
> java.lang.NullPointerException at
> ru.user.util.DBCPConnectionProvider.getConnection(DBCPConnectionProvider.java:158)
> at
> org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:279)
> at
> org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:124)
> at
> org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
> at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:206)
> at
> org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:178)
> at
> org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
> at
> org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
> at
> ru.user.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
> at ru.user.util.HibernateUtil.<clinit>(HibernateUtil.java:9) at
> ru.user.action.LoginAction.execute(LoginAction.java:43)
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties());
return configuration.buildSessionFactory(builder.build());
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
在 hibernate.cfg.xml 中
<property name="hibernate.connection.provider_class">
ru.user.util.DBCPConnectionProvider
</property>
和
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties());
return configuration.buildSessionFactory(builder.build()); //////// line 17
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
public class DBCPConnectionProvider implements ConnectionProvider {
private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory
.getLogger(DBCPConnectionProvider.class);
private static final String PREFIX = "hibernate.dbcp.";
private BasicDataSource ds;
// Old Environment property for backward-compatibility (property removed in
// Hibernate3)
private static final String DBCP_PS_MAXACTIVE = "hibernate.dbcp.ps.maxActive";
// Property doesn't exists in Hibernate2
private static final String AUTOCOMMIT = "hibernate.connection.autocommit";
public void configure(Properties props) throws HibernateException {
try {
log.debug("Configure DBCPConnectionProvider");
// DBCP properties used to create the BasicDataSource
Properties dbcpProperties = new Properties();
// DriverClass & url
String jdbcDriverClass = props.getProperty(Environment.DRIVER);
String jdbcUrl = props.getProperty(Environment.URL);
dbcpProperties.put("driverClassName", jdbcDriverClass);
dbcpProperties.put("url", jdbcUrl);
// Username / password
String username = props.getProperty(Environment.USER);
String password = props.getProperty(Environment.PASS);
dbcpProperties.put("username", username);
dbcpProperties.put("password", password);
// Isolation level
String isolationLevel = props.getProperty(Environment.ISOLATION);
if ((isolationLevel != null)
&& (isolationLevel.trim().length() > 0)) {
dbcpProperties.put("defaultTransactionIsolation",
isolationLevel);
}
// Turn off autocommit (unless autocommit property is set)
String autocommit = props.getProperty(AUTOCOMMIT);
if ((autocommit != null) && (autocommit.trim().length() > 0)) {
dbcpProperties.put("defaultAutoCommit", autocommit);
} else {
dbcpProperties.put("defaultAutoCommit",
String.valueOf(Boolean.FALSE));
}
// Pool size
String poolSize = props.getProperty(Environment.POOL_SIZE);
if ((poolSize != null) && (poolSize.trim().length() > 0)
&& (Integer.parseInt(poolSize) > 0)) {
dbcpProperties.put("maxActive", poolSize);
}
// Copy all "driver" properties into "connectionProperties"
Properties driverProps = ConnectionProviderInitiator
.getConnectionProperties(props);
if (driverProps.size() > 0) {
StringBuffer connectionProperties = new StringBuffer();
for (Iterator<?> iter = driverProps.entrySet().iterator(); iter
.hasNext();) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry) iter.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
connectionProperties.append(key).append('=').append(value);
if (iter.hasNext()) {
connectionProperties.append(';');
}
}
dbcpProperties.put("connectionProperties",
connectionProperties.toString());
}
// Copy all DBCP properties removing the prefix
for (Iterator<?> iter = props.entrySet().iterator(); iter.hasNext();) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry) iter.next();
String key = (String) entry.getKey();
if (key.startsWith(PREFIX)) {
String property = key.substring(PREFIX.length());
String value = (String) entry.getValue();
dbcpProperties.put(property, value);
}
}
// Backward-compatibility
if (props.getProperty(DBCP_PS_MAXACTIVE) != null) {
dbcpProperties.put("poolPreparedStatements",
String.valueOf(Boolean.TRUE));
dbcpProperties.put("maxOpenPreparedStatements",
props.getProperty(DBCP_PS_MAXACTIVE));
}
// Some debug info
if (log.isDebugEnabled()) {
log.debug("Creating a DBCP BasicDataSource with the following DBCP factory properties:");
StringWriter sw = new StringWriter();
dbcpProperties.list(new PrintWriter(sw, true));
log.debug(sw.toString());
}
// Let the factory create the pool
ds = (BasicDataSource) BasicDataSourceFactory
.createDataSource(dbcpProperties);
// The BasicDataSource has lazy initialization
// borrowing a connection will start the DataSource
// and make sure it is configured correctly.
Connection conn = ds.getConnection();
conn.close();
// Log pool statistics before continuing.
logStatistics();
} catch (Exception e) {
String message = "Could not create a DBCP pool";
log.error(message, e);
if (ds != null) {
try {
ds.close();
} catch (Exception e2) {
// ignore
}
ds = null;
}
throw new HibernateException(message, e);
}
log.debug("Configure DBCPConnectionProvider complete");
}
public Connection getConnection() throws SQLException {
Connection conn = null;
try {
conn = ds.getConnection(); //////// line 158
} finally {
logStatistics();
}
return conn;
}
public void closeConnection(Connection conn) throws SQLException {
try {
conn.close();
} finally {
logStatistics();
}
}
public void close() throws HibernateException {
log.debug("Close DBCPConnectionProvider");
logStatistics();
try {
if (ds != null) {
ds.close();
ds = null;
} else {
log.warn("Cannot close DBCP pool (not initialized)");
}
} catch (Exception e) {
throw new HibernateException("Could not close DBCP pool", e);
}
log.debug("Close DBCPConnectionProvider complete");
}
protected void logStatistics() {
if (log.isInfoEnabled()) {
// log.info("active: " + ds.getNumActive() + " (max: " +
// ds.getMaxTotal() + ") "
// + "idle: " + ds.getNumIdle() + "(max: " + ds.getMaxIdle() + ")");
}
}
public boolean supportsAggressiveRelease() {
return false;
}
@Override
public boolean isUnwrappableAs(
@SuppressWarnings("rawtypes") Class unwrapType) {
// TODO Auto-generated method stub
return false;
}
@Override
public <T> T unwrap(Class<T> unwrapType) {
// TODO Auto-generated method stub
return null;
}
}
请参阅代码中的注释以及堆栈跟踪中引用的行号。那么它有什么问题呢?我使用 dbcp2 池。
最佳答案
您引用的示例适用于旧版本的 Hibernate。它不适用于 Hibernate 4+,因为接口(interface) org.hibernate.connection.ConnectionProvider 不再存在。
我认为新方法是创建一个 org.apache.commons.dbcp.BasicDataSource
实例作为您的数据源。你可以找到一个例子here (向下滚动到“Apache Commons DBCP 示例”)。
也可以通过 Hibernate 配置来实现,如 this answer 所示。 .
关于java - Apache DBCP 连接池与 Hibernate 错误 : Initial SessionFactory creation failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27803797/
我使用的是基于 Eclipse 3.x 的 WID 6.1。 尝试右键单击文件夹时,新建-> 类,键入类的名称,然后单击完成,出现错误 "Creation of element failed. org
假设我想创建一个浏览器插件,每当某人单击我网页上的按钮时,该插件就会打开他们的软盘驱动器。 (你还记得那个老笑话吗?-别担心,我真的不想这么做。) 创建可在大多数浏览器和大多数操作系统上运行的浏览器插
我目前正在尝试在 Azure 容器实例上创建一个新容器,以在其上部署 .net core 应用程序镜像。 (我是这项技术的新手)。 我在 Azure 上创建了一个容器注册表,并在其上推送了一个镜像(.
我不确定我问的是否正确,但我知道你们都很聪明,能够弄清楚:)。我在压缩一些 python 类中的一些重复代码时遇到了麻烦。这是我的意思的一个例子...... class Parent: PAT
我想要扩展这些 C 预处理器行: #define _POUND_ #define _POUND_ _FALSE 0 _FALSE 展开,最后一行(即_FALSE)展开为0。我知道递归CPP不可能直接实
我需要创建 generic list可以充当List或 List因为我有以下情况。 if(e instanceof A) return A; else return B 但我想填充一个 List Re
我正在尝试构建一个共享对象,以便稍后在其他项目中使用共享对象中的函数 DoSomethingUseful()。它使用外部库以及我在多个项目中使用的一堆 header 。 使用 CMake,我创建了一个
void foo() { //some code MyClass m(); //some more code } C++ 标准是否确保 MyClass 类的构造函数将在 /
我正在尝试在 2D 游戏中创建斜坡。这个斜坡应该有一个 X 度。当现在一个球“滚过”这个斜坡时。这个坡度的效果应该改变,然后球的行为因此向后滚动,向前滚动或在其他地方初始化倾斜度。我认为我可以使用具有
我有一个开发分支,我想看看“分支创建”和现在的区别。实际上,我更感兴趣的是创建分支之后的提交,这更像是“创建分支”+1。 我知道我可以在 Git 命令中使用像 HEAD 和 HEAD~1 这样的简写。
在JavaScript: Understanding the Weird Parts讲师解释说,变量的内存是在所谓的创建阶段设置的(并且 undefined 已分配);然后执行阶段发生。但是,当我们不
我需要NodeJS代码来创建POST请求,以便从URL创建一个FB页面。必须代码有错误,我找不到它。错误消息为请求失败,状态代码为400。我没有包含accesToken字符串,但它存在于我的实际代码中
我开发了一个具有以下特点的 Fortran 代码: 全局变量 13 个带有多个子程序的模块 独立子程序 将英特尔 MKL 库用于 LAPACK 库(线性代数) 读写文本文件 代码变得相当大。即使在这个
我想创建一个数组 ArrayList .我尝试了以下方法: static ArrayList[] displayBlocks = new ArrayList[3]; 但是,我收到编译时错误: gene
云信息supports通过“堆栈创建选项”设置超时。但是这个部分在 serverless.yml 中?有什么解决方法吗? 最佳答案 查看actual code ,TimeoutInMinutes 参数
我们希望只有部分人能够在 Azure DevOps 云(?)存储库中创建“release/*”分支。 不幸的是,我在用户界面中找不到这个选项。我这里也找不到 https://learn.microso
我使用 Azure AD Graph API 差异查询来检测 Azure AD 中的更改。 对于 User 或 Group 类型的每个结果节点,如何区分该对象是新的还是仅更新的? 我使用ocp-aad
我正在发布简单的数据,例如 { "title" : "test Title" } 到(顺其自然)/posts uri,例如类似 RestAssured.baseURI = "http://localh
我正在尝试为撇号创建一个产品引擎。我在扩展“页面设置”表单时遇到问题,目前我想添加一个简单的文本区域来向页面添加概要 - 最终我想添加“产品”设置,但我需要先完成基础知识。 我创建了一个表单和一个设置
我使用 Azure AD Graph API 差异查询来检测 Azure AD 中的更改。 对于 User 或 Group 类型的每个结果节点,如何区分该对象是新的还是仅更新的? 我使用ocp-aad
我是一名优秀的程序员,十分优秀!