2015-03-02 53 views
2

我知道這個問題已被多次詢問過,所以我再次問道歉,但是,我已經嘗試了所有其他建議的修復,仍然無法讓這個工作。Tomcat 8 + MySQL + Spring + JPA - 無法創建用於連接URL的''''的''''''

我試圖創建一個使用MySQL數據庫的簡單Web應用程序,Tomcat的8,JNDI數據源,Spring和JPA,但我得到部署以下錯誤:

Cannot create JDBC driver of class '' for connect URL 'null' 
java.sql.SQLException: No suitable driver 
    at java.sql.DriverManager.getDriver(DriverManager.java:315) 
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2049) 
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1939) 
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412) 
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139) 
    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: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) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845) 
    at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844) 
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75) 
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55) 
    at org.springframework.orm.jpa.LocalEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalEntityManagerFactoryBean.java:96) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108) 
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:444) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1469) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) 

這裏是我的配置:

的content.xml

<?xml version="1.0" encoding="UTF-8"?> 
<Context> 
    <Resource 
    name="jdbc/testapp" 
    auth="Container" 
    type="javax.sql.DataSource" 
    maxTotal="100" 
    maxIdle="30" 
    maxWaitMillis="10000" 
    username="test" 
    password="password123" 
    driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://localhost:3306/test"/> 
</Context> 

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" 
xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd"> 

    <persistence-unit name="org.iainadams.testappdb" transaction-type="RESOURCE_LOCAL"> 
     <description>Default MySQL database</description> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <jta-data-source>java:comp/env/jdbc/testapp</jta-data-source> 
     <properties> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> 
     </properties> 
    </persistence-unit> 

</persistence> 

的web.xml

.... 
<resource-ref> 
    <description>MySQL Database</description> 
    <res-ref-name>jdbc/testapp</res-ref-name> 
    <res-type>javax.sql.DataSource</res-type> 
    <res-auth>Container</res-auth> 
</resource-ref> 
.... 

org.iainadams.testapp

import javax.persistence.EntityManagerFactory; 
import javax.sql.DataSource; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.jndi.JndiObjectFactoryBean; 
import org.springframework.orm.jpa.JpaTransactionManager; 
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean; 
import org.springframework.orm.jpa.vendor.Database; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.view.tiles3.TilesConfigurer; 
import org.springframework.web.servlet.view.tiles3.TilesViewResolver; 

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages="org.iainadams.testapp") 
@EnableJpaRepositories("org.iainadams.testapp.repositories") 
@EnableTransactionManagement 
public class ApplicationConfig { 

    @Bean 
    public TilesConfigurer tilesConfigurer(){ 
     TilesConfigurer tilesConfigurer = new TilesConfigurer(); 
     tilesConfigurer.setDefinitions("/WEB-INF/tiles/tiles-defs.xml"); 
     return tilesConfigurer; 
    } 

    @Bean 
    public TilesViewResolver viewResolver(){ 
     return new TilesViewResolver(); 
    } 

    @Bean(name="entityManagerFactory") 
    public LocalEntityManagerFactoryBean entityManagerFactory(){ 
     LocalEntityManagerFactoryBean localEntityManagerFactory = new LocalEntityManagerFactoryBean(); 
     localEntityManagerFactory.setPersistenceUnitName("org.iainadams.testappdb"); 
     return localEntityManagerFactory; 
    } 

    @Bean 
    public DataSource datasource(){ 
     JndiObjectFactoryBean datasourceLookup = new JndiObjectFactoryBean(); 
     datasourceLookup.setResourceRef(true); 
     datasourceLookup.setJndiName("java:comp/env/jdbc/testapp"); 
     return (DataSource) datasourceLookup.getObject(); 
    } 


    @Bean 
    public JpaTransactionManager txManager(EntityManagerFactory emf){ 
     JpaTransactionManager txManager = new JpaTransactionManager(); 
     txManager.setEntityManagerFactory(emf); 
     return txManager; 
    } 

} 

另外,我已經把我的MySQL驅動jar文件(mysql的,連接器 - java-5.1.34-bin.jar)放入apache-tomcat-8.0.18 \ lib文件夾。

Tomcat Lib

任何幫助將非常感激。

+0

我要說的是,文件_content.xml_是找不到的,因爲你已經有定義的屬性。在實際例外之前,你可能會得到一些警告嗎? – Magnilex 2015-03-02 15:51:48

+0

沒有警告。雖然我確實意識到它正在部署到/ WEB-INF/classes/META-INF /以及persistence.xml。我讀到它需要直接在/ META-INF下。所以我已經更新了我的項目配置。但是,它仍然沒有正確部署。我仍然得到同樣的錯誤。 – user2294382 2015-03-02 18:45:25

+0

好的....一些進步....如果我把context.xml中的描述放在全局context.xml中(在$ CATALINA_HOME/conf/context.xml下),那麼它就可以工作。我認爲你是對的,因爲我的context.xml沒有被服務器識別。 – user2294382 2015-03-02 21:39:59

回答

0

一)它應該是java:/ comp/env的/ JDBC/testapp(失蹤的java後斜線)

B)我相信這是因爲Tomcat不提供JTA事務(糾正我,如果我錯)

0

你可以試試這個下面的Java配置

@Bean 
public BasicDataSource dataSource(){ 
     BasicDataSource basicDataSource=new BasicDataSource(); 
     basicDataSource.addConnectionProperty("destroy-method","close"); 
     basicDataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
     basicDataSource.setUsername("root"); 
     basicDataSource.setUrl("jdbc:mysql://localhost/dbname"); 
     basicDataSource.setPassword(""); 
     return basicDataSource; 
} 

@Bean 
public AnnotationSessionFactoryBean sessionFactoryBean(){ 
     AnnotationSessionFactoryBean asfb=new AnnotationSessionFactoryBean(); 
     asfb.setDataSource(dataSource()); 
     asfb.setConfigLocation(new ClassPathResource("hibernate.cfg.xml")); 
     asfb.setHibernateProperties(hibernateProperties()); 

     return asfb; 
} 
public SessionFactory sessionFactory(){ 
     return sessionFactoryBean().getObject(); 
} 
private Properties hibernateProperties(){ 
     Properties properties=new Properties(); 
     properties.put("dialect","org.hibernate.dialect.MySQLDialect"); 

     return properties; 
} 
+0

謝謝@LynAs。儘管我非常希望使用JPA和JNDI,但如果能夠使其運行起來,這對於回退是非常有用的。 – user2294382 2015-03-02 21:41:06

+0

使用jpa和jndi沒有問題。我給了你java配置,所以你可以更好地調試。改變司機和方言,它會和你一起工作 – LynAs 2015-03-03 04:51:25

相關問題