jdk1.4+hibernate 3.0,连接池用的c3p0,在获取session的时候为null,页面上报java.lang.NullPointerException,报错行所在的方法的代码如下: public void visitsAddition() {
log.debug("开始执行visitsAddition方法!");
Session session = null;
Transaction tr = null;
try {
Visits vi = findByVDate(getNowDate());
System.out.println("come here");
session = HibernateUtil.currentSession();
System.out.println(session.isConnected());
tr = session.beginTransaction();
int dayVisits = vi.getVDayVisits() + 1;
int totalVisits = vi.getVTotalVisits() + 1;
vi.setVDayVisits(dayVisits);
vi.setVTotalVisits(totalVisits);
System.out.println(session.isConnected());
session.update(vi);
System.out.println(session.isConnected());
tr.commit();
log.debug("执行visitsAddition成功!");
} catch (Exception e) {
log.error("visitsAddition方法中发生异常", e);
if (tr != null)
tr.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
HibernateUtil.javapublic class HibernateUtil { private static final SessionFactory sessionFactory; static {
try {
sessionFactory = new Configuration().configure("/com/visits/hibernate/hibernate.cfg.xml")
.buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: "
+ ex.getMessage(), ex);
}
} public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
} public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}hibernate.cfg.xml:<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration> <session-factory>
<property name="connection.username">sa</property>
<property name="connection.url">
jdbc:jtds:sqlserver://localhost:1433/Visits
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="myeclipse.connection.profile">
visitsDriver
</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">
net.sourceforge.jtds.jdbc.Driver
</property>

<!-- C3P0数据库连接池 -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">5000</property>
<property name="c3p0.timeout">180000</property>
<property name="c3p0.max_statements">100</property>

<mapping resource="com/visits/hibernate/po/Visits.hbm.xml" />
</session-factory>
</hibernate-configuration>
以前是这样配的没问题,不知道现在为什么报错,请大家帮忙解答,谢谢!

解决方案 »

  1.   

    这是应用启动后,初始化连接池打印出的信息:
    Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@75c2be [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@1a072a6
     [ acquireIncrement -> 1,
     acquireRetryAttempts -> 30,
     acquireRetryDelay -> 1000,
     autoCommitOnClose -> false,
     automaticTestTable -> null,
     breakAfterAcquireFailure -> false,
     checkoutTimeout -> 0,
     connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester,
     factoryClassLocation -> null,
     forceIgnoreUnresolvedTransactions -> false,
     idleConnectionTestPeriod -> 0,
     initialPoolSize -> 5,
     maxIdleTime -> 180000,
     maxPoolSize -> 5000,
     maxStatements -> 100,
     maxStatementsPerConnection -> 0,
     minPoolSize -> 5,
     nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@640b25
     [ description -> null,
     driverClass -> null,
     factoryClassLocation -> null,
     jdbcUrl -> jdbc:jtds:sqlserver://localhost:1433/Visits,
     properties -> {user=******, password=******} ] ,
     preferredTestQuery -> null,
     propertyCycle -> 300,
     testConnectionOnCheckin -> false,
     testConnectionOnCheckout -> false,
     usesTraditionalReflectiveProxies -> false ] ,
     factoryClassLocation -> null,
     numHelperThreads -> 3,
     poolOwnerIdentityToken -> 75c2be ] 
      

  2.   

    vi.setVDayVisits(dayVisits);
    vi.setVTotalVisits(totalVisits);不是session的问题吧?是空指针异常你debug这两句看看
      

  3.   

    hibernate会打印出详细的日志信息,把日志贴出来,如果没有日志在src下面增加一下文件:log4j.properties
      

  4.   

    2009-12-07 09:23:19,843-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Environment - Hibernate 3.0.5
    2009-12-07 09:23:19,843-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Environment - hibernate.properties not found
    2009-12-07 09:23:19,843-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Environment - using CGLIB reflection optimizer
    2009-12-07 09:23:19,843-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
    2009-12-07 09:23:20,187-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Configuration - configuring from resource: /com/visits/hibernate/hibernate.cfg.xml
    2009-12-07 09:23:20,187-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Configuration - Configuration resource: /com/visits/hibernate/hibernate.cfg.xml
    2009-12-07 09:23:20,531-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Configuration - Mapping resource: com/visits/hibernate/po/Visits.hbm.xml
    2009-12-07 09:23:20,843-[TS] INFO http-8082-Processor25 org.hibernate.cfg.HbmBinder - Mapping class: com.visits.hibernate.po.Visits -> Visits
    2009-12-07 09:23:20,953-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Configuration - Configured SessionFactory: null
    2009-12-07 09:23:20,953-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Configuration - processing extends queue
    2009-12-07 09:23:20,953-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Configuration - processing collection mappings
    2009-12-07 09:23:20,953-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Configuration - processing association property references
    2009-12-07 09:23:20,953-[TS] INFO http-8082-Processor25 org.hibernate.cfg.Configuration - processing foreign key constraints
    2009-12-07 09:23:21,265-[TS] INFO http-8082-Processor25 org.hibernate.connection.C3P0ConnectionProvider - C3P0 using driver: net.sourceforge.jtds.jdbc.Driver at URL: jdbc:jtds:sqlserver://localhost:1433/Visits
    2009-12-07 09:23:21,265-[TS] INFO http-8082-Processor25 org.hibernate.connection.C3P0ConnectionProvider - Connection properties: {user=sa, password=****}
    2009-12-07 09:23:21,265-[TS] INFO http-8082-Processor25 org.hibernate.connection.C3P0ConnectionProvider - autocommit mode: false
    2009-12-07 09:23:22,296-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - RDBMS: Microsoft SQL Server, version: 08.00.2039
    2009-12-07 09:23:22,296-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - JDBC driver: jTDS Type 4 JDBC Driver for MS SQL Server and Sybase, version: 1.2.2
    2009-12-07 09:23:22,437-[TS] INFO http-8082-Processor25 org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.SQLServerDialect
    2009-12-07 09:23:22,453-[TS] INFO http-8082-Processor25 org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
    2009-12-07 09:23:22,468-[TS] INFO http-8082-Processor25 org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    2009-12-07 09:23:22,468-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
    2009-12-07 09:23:22,468-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
    2009-12-07 09:23:22,468-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
    2009-12-07 09:23:22,468-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
    2009-12-07 09:23:22,468-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Connection release mode: null
    2009-12-07 09:23:22,484-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
    2009-12-07 09:23:22,484-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
    2009-12-07 09:23:22,484-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
    2009-12-07 09:23:22,484-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    2009-12-07 09:23:22,640-[TS] INFO http-8082-Processor25 org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
    2009-12-07 09:23:22,640-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
    2009-12-07 09:23:22,640-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
    2009-12-07 09:23:22,640-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Query cache: disabled
    2009-12-07 09:23:22,640-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Cache provider: org.hibernate.cache.EhCacheProvider
    2009-12-07 09:23:22,671-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
    2009-12-07 09:23:22,671-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
    2009-12-07 09:23:22,703-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Statistics: disabled
    2009-12-07 09:23:22,703-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
    2009-12-07 09:23:22,703-[TS] INFO http-8082-Processor25 org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
    2009-12-07 09:23:23,140-[TS] INFO http-8082-Processor25 org.hibernate.impl.SessionFactoryImpl - building session factory
    2009-12-07 09:23:23,187-[TS] WARN http-8082-Processor25 net.sf.ehcache.config.Configurator - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: file:/D:/TRS/TRSWCMV6/Tomcat/work/Catalina/localhost/VisitsCount/loader/ehcache-failsafe.xml
    2009-12-07 09:23:24,375-[TS] INFO http-8082-Processor25 org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
    2009-12-07 09:23:24,375-[TS] INFO http-8082-Processor25 org.hibernate.impl.SessionFactoryImpl - Checking 0 named queries这是我启动一次log4j打印出的日志
      

  5.   

    你把jdk1.4换jdk1.5以上试试,我看了你的代码,没看出来有什么错误。java.lang.NullPointerException也没找到
      

  6.   

    别人服务器上用的JDK1.4,我没有权力改别人服务器上的环境