gpt4 book ai didi

C3P0ConnectionProvider.getConnection() 中的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 13:47:02 26 4
gpt4 key购买 nike

在我的应用程序中,我在最新的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com