初始SessionFactory创建失败.java.lang.NoClassDefFoundError:org / hibernate / service / jdbc / connections / spi / ConnectionProvider

时间:2015-05-12 06:40:15

标签: java hibernate bonecp

尝试将hibernate版本3.6 ..更新为4.3。*

时出现以下错误
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/hibernate/service/jdbc/connections/spi/ConnectionProvider
java.lang.NoClassDefFoundError: org/hibernate/service/jdbc/connections/spi/ConnectionProvider
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:415)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:377)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:224)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:242)
    at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:125)
    at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.instantiateExplicitConnectionProvider(ConnectionProviderInitiator.java:194)
    at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:120)
    at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:55)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:105)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:251)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:225)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)

我的Hibernate Util类是

private static SessionFactory buildSessionFactory() {
        try {
            SessionFactory sf = null;
            Configuration configuration = new Configuration().configure();
            //From the properties file
            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                    configuration.getProperties()).build();
            if (StringUtils.isNotBlank(Config.getHibernateConfigFile())) {
                ClassLoader loader = HibernateUtil.class.getClassLoader();
                if (loader == null)
                    loader = ClassLoader.getSystemClassLoader();

                java.net.URL configURL = loader.getResource(Config.getHibernateConfigFile());
                if (null != configURL) {
                    log.info("Configuring hibernate using " + Config.getHibernateConfigFile() + " located at " + configURL.toString());
//                 sf = new Configuration().configure(configURL).buildSessionFactory();
                    configuration = configuration.configure(configURL);
                    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
                    sf = configuration.buildSessionFactory(serviceRegistry);

                } else
                    log.warn("Config file not found " + Config.getHibernateConfigFile());
            }
            //Default hibernate.cfg.xml from resource
            if (null == sf) {
                sf = configuration.buildSessionFactory(serviceRegistry);
            }
            return sf;
        } 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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/dummyDb?useLegacyDatetimeCode=false</property>
        <property name="connection.username">bobby</property>
        <property name="connection.password"></property>


        <!-- BonceCP connection pool -->
        <property name="hibernate.connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider
        </property>
        <property name="bonecp.setIdleMaxAgeInMinutes">60</property>
        <property name="bonecp.setIdleConnectionTestPeriodInMinutes">5</property>
        <property name="bonecp.partitionCount">3</property>
        <property name="bonecp.acquireIncrement">2</property>
        <property name="bonecp.maxConnectionsPerPartition">5</property>
        <property name="bonecp.minConnectionsPerPartition">2</property>
        <property name="bonecp.statementsCacheSize">50</property>
        <property name="bonecp.releaseHelperThreads">3</property>
        <property name="connection.autoReconnect">true</property>
        <property name="connection.autoReconnectForPools">true</property>
        <!--<property name="bonecp.idleConnectionTestPeriodInMinutes">60</property>-->
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

我的pom.xml是

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.9.Final</version>
        </dependency>
 <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp</artifactId>
            <version>0.8.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.jolbox</groupId>
            <artifactId>bonecp-provider</artifactId>
            <version>0.8.0-alpha1</version>
        </dependency>

1 个答案:

答案 0 :(得分:0)

Bonecp ConnectionProvider的问题,Hibernate 4.3.9需要来自包源的连接提供程序,该包被移动到bonecp-0.8.0.alpha中的另一个包,这会导致问题

删除了bonecp并使用了hibernate 4.3。*默认支持的hikaricp

**注意:来自hikari的是hibernate 4.3 *的默认支持者