Spring-hibernate:通过tomcat jndi的dataSource

时间:2012-08-22 17:56:54

标签: java spring hibernate tomcat jndi

看起来问题很老,但我找不到正确的解决方案。

细节:

的web.xml

    <resource-ref>
    <res-ref-name>jdbc/jndisqldbhello</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

context.xml中

<Context>
<Resource name="jdbc/jndisqldbhello"
    auth="Container"
    type="javax.sql.DataSource"
    factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
    maxActive="10"
    maxIdle="5"
    maxWait="10000"
    username="root"
    password="123456"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/javatest"
    validationQuery="select 1"/>
</Context>

的applicationContext.xml

<jee:jndi-lookup jndi-name="jdbc/jndisqldbhello" id="dataSource" />
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="dataSource" ref="dataSource" />
</bean>

的persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
    </properties>
</persistence-unit>
</persistence>

无异常地启动TOMCAT,但我对有关NULL定义对象的警告感到困惑:

2011-04-08 20:32:30,311 - INFO ContextLoader - Root WebApplicationContext: initialization started
2011-04-08 20:32:30,576 - INFO XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Fri Apr 08 20:32:30 EEST 2011]; root of context hierarchy
2011-04-08 20:32:30,770 - INFO XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [META-INF/spring/applicationContext.xml]
***2011-04-08 20:32:33,082 - INFO XmlWebApplicationContext - Bean 'dataSource' of type [class org.springframework.jndi.JndiObjectFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)***
***2011-04-08 20:32:33,087 - INFO XmlWebApplicationContext - Bean 'dataSource' of type [class org.apache.tomcat.dbcp.dbcp.BasicDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)***
2011-04-08 20:32:33,139 - INFO LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'persistenceUnit'
2011-04-08 20:32:33,687 - INFO Version - HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
2011-04-08 20:32:33,716 - INFO Version - HHH000412: Hibernate Core {4.1.5.Final}
2011-04-08 20:32:33,730 - INFO Environment - HHH000206: hibernate.properties not found
2011-04-08 20:32:33,738 - INFO Environment - HHH000021: Bytecode provider name : javassist
2011-04-08 20:32:33,920 - INFO Ejb3Configuration - HHH000204: Processing PersistenceUnitInfo [
    name: persistenceUnit
    ...]
2011-04-08 20:32:34,738 - INFO ConnectionProviderInitiator - HHH000130: Instantiating explicit connection provider: org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider
***2011-04-08 20:32:34,836 - WARN JdbcServicesImpl - HHH000342: Could not obtain connection to query metadata : Cannot create JDBC driver of class '' for connect URL 'null'***
2011-04-08 20:32:34,865 - INFO Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
***2011-04-08 20:32:34,903 - INFO LobCreatorBuilder - HHH000422: Disabling contextual LOB creation as connection was null***
2011-04-08 20:32:34,952 - INFO TransactionFactoryInitiator - HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
2011-04-08 20:32:34,972 - INFO ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory
2011-04-08 20:32:35,193 - INFO Version - HV000001: Hibernate Validator 4.3.0.Final
***2011-04-08 20:32:36,437 - INFO XmlWebApplicationContext - Bean 'entityManagerFactory' of type [class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)***
2011-04-08 20:32:36,511 - INFO DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f474205: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.context.config.internalBeanConfigurerAspect,userRepository,org.springframework.data.repository.core.support.RepositoryInterfaceAwareBeanPostProcessor#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,mySimpleDBService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,transactionManager,org.springframework.transaction.config.internalTransactionAspect,entityManagerFactory,dataSource,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
2011-04-08 20:32:37,004 - INFO ContextLoader - Root WebApplicationContext: initialization completed in 6692 ms
апр 08, 2011 8:32:37 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'webapp'
2011-04-08 20:32:37,107 - INFO DispatcherServlet - FrameworkServlet 'webapp': initialization started
2011-04-08 20:32:37,120 - INFO XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'webapp-servlet': startup date [Fri Apr 08 20:32:37 EEST 2011]; parent: Root WebApplicationContext
2011-04-08 20:32:37,126 - INFO XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/webapp-servlet.xml]
2011-04-08 20:32:37,459 - INFO DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@654309f0: defining beans [restExampleController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@f474205
2011-04-08 20:32:37,696 - INFO RequestMappingHandlerMapping - Mapped "{[/test/hi],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.first.web.controller.RestExampleController.getHi()
2011-04-08 20:32:37,697 - INFO RequestMappingHandlerMapping - Mapped "{[/test/hello],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.first.web.controller.RestExampleController.getMovies(org.springframework.ui.ModelMap)
2011-04-08 20:32:38,992 - INFO DispatcherServlet - FrameworkServlet 'webapp': initialization completed in 1884 ms
апр 08, 2011 8:32:39 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]

它开始没有例外,但我相信我会得到它们。 如果我加入 persistance.xml

<property name="hibernate.hbm2ddl.auto" value="update"/>

我有例外:

    2011-04-08 20:50:17,160 - INFO Version - HV000001: Hibernate Validator 4.3.0.Final
2011-04-08 20:50:18,221 - INFO SchemaUpdate - HHH000228: Running hbm2ddl schema update
2011-04-08 20:50:18,222 - INFO SchemaUpdate - HHH000102: Fetching database metadata
2011-04-08 20:50:18,227 -ERROR SchemaUpdate - HHH000319: Could not get database metadata
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:70)
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:194)
    at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:178)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:492)
    ....
2011-04-08 20:50:18,238 -ERROR SchemaUpdate - HHH000299: Could not complete schema update
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
    at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
    at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:70)
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)

有人可以帮我理解应该如何解决吗?

1 个答案:

答案 0 :(得分:2)

似乎解决方案在最近的topic

由于hibernate issue

,我第一次不清楚(不起作用)

如果我在persistence.xml中使用

,我发现一般问题已经解决了
java://comp/env/jdbc/myDatabase

例如

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java://comp/env/jdbc/jndisqldbhello</non-jta-data-source>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.connection.datasource" value="java://comp/env/jdbc/jndisqldbhello"/>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
    </properties>
</persistence-unit>
</persistence>

但如果我添加属性

<property name="hibernate.hbm2ddl.auto" value="create"/>

它不起作用((。

如果有任何建议请通知我,谢谢。