- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我在最新的 hibernate jar 文件中的 C3P0ConnectionProvider.getConnection() 方法中收到 java.lang.NullPointerException
使用的 Jar 文件是
hibernate-core-4.3.0.Beta5.jar
hibernate-c3p0-4.3.0.Beta5.jar
hibernate-commons-annotations-4.0.4.Final.jar
hibernate-entitymanager-4.3.0.Beta5.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
slf4j-api-1.7.5.jar
slf4j-simple-1.7.5.jar
jta-1.1.jar
jboss-logging-3.1.3.GA.jar
javax.persistence-2.1.0.jar
下面是我的 C3P0ConnectionProvider 类代码
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.internal.util.ReflectHelper;
import com.mchange.v2.c3p0.DataSources;
public class C3P0ConnectionProvider implements ConnectionProvider {
private static final Log log = LogFactory.getLog(C3P0ConnectionProvider.class);
private final static String C3P0_STYLE_MIN_POOL_SIZE = "c3p0.minPoolSize";
private final static String C3P0_STYLE_MAX_POOL_SIZE = "c3p0.maxPoolSize";
private final static String C3P0_STYLE_MAX_IDLE_TIME = "c3p0.maxIdleTime";
private final static String C3P0_STYLE_MAX_STATEMENTS = "c3p0.maxStatements";
private final static String C3P0_STYLE_ACQUIRE_INCREMENT = "c3p0.acquireIncrement";
private final static String C3P0_STYLE_IDLE_CONNECTION_TEST_PERIOD = "c3p0.idleConnectionTestPeriod";
private final static String C3P0_STYLE_INITIAL_POOL_SIZE = "c3p0.initialPoolSize";
private DataSource ds;
private Integer isolation;
private boolean autocommit;
/**
* {@inheritDoc}
*/
public Connection getConnection() throws SQLException {
final Connection c = ds.getConnection();
if (isolation != null) {
c.setTransactionIsolation(isolation.intValue());
}
if (c.getAutoCommit() != autocommit) {
c.setAutoCommit(autocommit);
}
return c;
}
/**
* {@inheritDoc}
*/
public void closeConnection(Connection conn) throws SQLException {
conn.close();
}
/**
* {@inheritDoc}
*/
public void configure(Properties props) throws HibernateException {
String jdbcDriverClass = props.getProperty(Environment.DRIVER);
String jdbcUrl = props.getProperty(Environment.URL);
Properties connectionProps = ConnectionProviderInitiator.getConnectionProperties(props);
autocommit = ConfigurationHelper.getBoolean(Environment.AUTOCOMMIT, props);
if (jdbcDriverClass == null) {
log.warn("No JDBC Driver class was specified by property "
+ Environment.DRIVER);
} else {
try {
Class.forName(jdbcDriverClass);
} catch (ClassNotFoundException cnfe) {
try {
ReflectHelper.classForName(jdbcDriverClass);
} catch (ClassNotFoundException e) {
String msg = "JDBC Driver class not found: "
+ jdbcDriverClass;
log.fatal(msg, e);
throw new HibernateException(msg, e);
}
}
}
try {
Integer minPoolSize = ConfigurationHelper.getInteger(
Environment.C3P0_MIN_SIZE, props);
Integer maxPoolSize = ConfigurationHelper.getInteger(
Environment.C3P0_MAX_SIZE, props);
Integer maxIdleTime = ConfigurationHelper.getInteger(
Environment.C3P0_TIMEOUT, props);
Integer maxStatements = ConfigurationHelper.getInteger(
Environment.C3P0_MAX_STATEMENTS, props);
Integer acquireIncrement = ConfigurationHelper.getInteger(
Environment.C3P0_ACQUIRE_INCREMENT, props);
Integer idleTestPeriod = ConfigurationHelper.getInteger(
Environment.C3P0_IDLE_TEST_PERIOD, props);
Properties c3props = new Properties();
// turn hibernate.c3p0.* into c3p0.*, so c3p0
// gets a chance to see all hibernate.c3p0.*
for (Iterator<?> ii = props.keySet().iterator(); ii.hasNext();) {
String key = (String) ii.next();
if (key.startsWith("hibernate.c3p0.")) {
String newKey = key.substring(10);
if (props.containsKey(newKey)) {
warnPropertyConflict(key, newKey);
}
c3props.put(newKey, props.get(key));
}
}
setOverwriteProperty(Environment.C3P0_MIN_SIZE,
C3P0_STYLE_MIN_POOL_SIZE, props, c3props, minPoolSize);
setOverwriteProperty(Environment.C3P0_MAX_SIZE,
C3P0_STYLE_MAX_POOL_SIZE, props, c3props, maxPoolSize);
setOverwriteProperty(Environment.C3P0_TIMEOUT,
C3P0_STYLE_MAX_IDLE_TIME, props, c3props, maxIdleTime);
setOverwriteProperty(Environment.C3P0_MAX_STATEMENTS,
C3P0_STYLE_MAX_STATEMENTS, props, c3props, maxStatements);
setOverwriteProperty(Environment.C3P0_ACQUIRE_INCREMENT,
C3P0_STYLE_ACQUIRE_INCREMENT, props, c3props,
acquireIncrement);
setOverwriteProperty(Environment.C3P0_IDLE_TEST_PERIOD,
C3P0_STYLE_IDLE_CONNECTION_TEST_PERIOD, props, c3props,
idleTestPeriod);
// revert to traditional hibernate behavior of setting
// initialPoolSize to minPoolSize
// unless otherwise specified with a c3p0.*-style parameter.
Integer initialPoolSize = ConfigurationHelper.getInteger(
C3P0_STYLE_INITIAL_POOL_SIZE, props);
if (initialPoolSize == null && minPoolSize != null) {
c3props.put(C3P0_STYLE_INITIAL_POOL_SIZE, String.valueOf(
minPoolSize).trim());
}
/*
* DataSource unpooled = DataSources.unpooledDataSource( jdbcUrl,
* props.getProperty(Environment.USER),
* props.getProperty(Environment.PASS) );
*/
DataSource unpooled = DataSources.unpooledDataSource(jdbcUrl,
connectionProps);
Properties allProps = (Properties) props.clone();
allProps.putAll(c3props);
ds = DataSources.pooledDataSource(unpooled, allProps);
} catch (Exception e) {
log.fatal("could not instantiate C3P0 connection pool", e);
throw new HibernateException(
"Could not instantiate C3P0 connection pool", e);
}
String i = props.getProperty(Environment.ISOLATION);
if (i == null) {
isolation = null;
} else {
isolation = new Integer(i);
log.info("JDBC isolation level: "
+ Environment.isolationLevelToString(isolation.intValue()));
}
}
/**
* {@inheritDoc}
*/
public void close() {
try {
DataSources.destroy(ds);
} catch (SQLException sqle) {
log.warn("could not destroy C3P0 connection pool", sqle);
}
}
/**
* {@inheritDoc}
*/
public boolean supportsAggressiveRelease() {
return false;
}
private void setOverwriteProperty(String hibernateStyleKey,
String c3p0StyleKey, Properties hibp, Properties c3p, Integer value) {
if (value != null) {
c3p.put(c3p0StyleKey, String.valueOf(value).trim());
if (hibp.getProperty(c3p0StyleKey) != null) {
warnPropertyConflict(hibernateStyleKey, c3p0StyleKey);
}
String longC3p0StyleKey = "hibernate." + c3p0StyleKey;
if (hibp.getProperty(longC3p0StyleKey) != null) {
warnPropertyConflict(hibernateStyleKey, longC3p0StyleKey);
}
}
}
private void warnPropertyConflict(String hibernateStyle, String c3p0Style) {
log.warn("Both hibernate-style property '" + hibernateStyle
+ "' and c3p0-style property '" + c3p0Style
+ "' have been set in hibernate.properties. "
+ "Hibernate-style property '" + hibernateStyle
+ "' will be used " + "and c3p0-style property '" + c3p0Style
+ "' will be ignored!");
}
/**
* @return the ds
*/
public DataSource getDs() {
return ds;
}
@Override
public boolean isUnwrappableAs(Class arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public <T> T unwrap(Class<T> arg0) {
// TODO Auto-generated method stub
return null;
}
}
在hibernate.cfg.xml中,我已经配置了c3p0
<property name="hibernate.connection.provider_class">C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.max_size">1000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.min_size">50</property>
<property name="hibernate.c3p0.timeout">20</property>
在 public Connection getConnection() 方法中的 ds.getConnection() 第 43 行中获取 java.lang.NullPointerException。
这是我的异常堆栈跟踪
Caused by: java.lang.NullPointerException
at C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:60)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:284)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:129)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:89)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1881)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1839)
at HibernateUtil.getFactory(HibernateUtil.java:43)
at HibernateUtil.openSession(HibernateUtil.java:64)
at HibernateUtil.currentSession(HibernateUtil.java:56)
at openSession(CacheDaoImpl.java:404)
at loadCache(CacheDaoImpl.java:94)
at init(CacheDaoImpl.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1414)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1375)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
提前致谢。
最佳答案
看起来您的变量ds为空。
您确定在 getConnection() 之前调用 configure(Properties props) 吗?因为有变量ds的初始化。
关于C3P0ConnectionProvider.getConnection() 中的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20290924/
Spring Boot 应用程序使用 Azure Cosmos DB 启动失败 描述: 试图调用不存在的方法。尝试是从以下位置进行的: com.azure.cosmos.implementation.
要执行一些自定义(黑盒化)SQL 操作,我需要有一个 IDBConnection 实例到我们已经通过 NHibernate 定期访问的数据库。 我不想部署单独的数据库配置(一个本地的,一个 nhibe
我刚刚将应用程序中的 Hibernate 更新到 4.3.4(从 3.X),这导致了一些问题。第一个是我解决的创建 sessionFactory 的新方法。 下一个问题,我什么也没发现,是我现在在 o
如何构建/获取 ConnectionProvider在 Spring(v.3.1.1) + Hibernate(v.4.1.1) + C3P0 环境中? 背景: 在 Spring(v.3.1.1) +
如何从 hibernate 5 中的 session 工厂获取连接提供程序?获取连接的方法不再存在,并且不会被 javadoc 中的任何内容替换。此代码片段在 4.1 中有效,但在 5.1 中无效(具
我正在升级 kotlin Spring Boot 1.x 应用程序以使用 Spring Boot 2.1.6 之前,我使用 EntityManagerFactoryBeanCallback 将 hib
我做错了什么?我认为,问题出在 hibernate-core-3.5.6-Final-patched-play-1.1.1 库中。但我不知道从哪里获得连接包和 ConnectionProvider 的
hibernate .cfg.xml oracle.jdbc.driver.OracleDriver cisadm jdbc:oracle:
我正在尝试将旧版 Web 应用程序从 JBoss 4.2.3GA 迁移到 Wildfly 9。 此迁移的一部分需要从 Hibernate 3.2 升级到 Hibernate 4.3.10 我对 Hib
我是一名优秀的程序员,十分优秀!