Spring source suite spring3 + Hibernate4 + maven 3 + MySQL 5

后端 未结 5 1018
南笙
南笙 2021-02-05 18:22

I am getting an error in my application but I\'m not sure what it means. I posted some sample code bellow. This is the error I get:

> SEVERE: Exception sendin         


        
相关标签:
5条回答
  • 2021-02-05 18:44

    This article suggests that you don't have a datasource defined in your hibernate config file.

    0 讨论(0)
  • 2021-02-05 18:47

    I've exactly the same problem. KyelJmD suggests here that there is a missing connection pooling, and it seems he is right, but he didn't post solution.

    So I've spent some time and finaly get it working. To solve this problem you should do two things:

    1. add these lines into hibernate.cfg.xml file (it's possible that you don't need all of it):

      <property name="hibernate.c3p0.min_size">5</property>
      <property name="hibernate.c3p0.max_size">20</property>
      <property name="hibernate.c3p0.timeout">300</property>
      <property name="hibernate.c3p0.max_statements">50</property>
      <property name="hibernate.c3p0.idle_test_period">3000</property>
      
    2. Add this dependencies in pom.xml:

      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-c3p0</artifactId>
          <version>4.1.9.Final</version>
      </dependency>
      
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-ehcache</artifactId>
          <version>4.1.9.Final</version>
      </dependency>
      

    Without maven dependency Hibernate just silently ignores C3P0 configuration.

    With above configuration I dont't get anymore UnknownUnwrapTypeException exception, and it seems it works fine now.

    My setup: Spring 3.2.1, Hibernate 4.1.9, Tomcat 7.0.35.

    Dislaimer: as stated in this question, you should rather not use hibernate.cfg.xml file. Use dataSource bean instead, because, for example, you can use it later for JdbcTemplate.

    EDIT :

    It's been a while, as I answered this question. It seemed correct, but I never like it, because it was kind of guess, which we should really avoid.

    So, because I found this interresting and I was unable to find anything in Hibernate 4.1 docs, I tried to proof my and other commenters "guess".

    Here is a step-by-step procedure, probably also useful for solving other weird issues in other libraries. Turns out this time it was really easy, what is going on.

    1. Create a Maven Project (or use existing one), add Hibernate to dependency (hibernate-entitymanager, I used this time version 4.1.7). Import it into IDE which can automatically attach source code (I used Intellij)
    2. From Stack Trace, find the class where exception (UnknownUnwrapTypeException) has been thrown (DriverManagerConnectionProviderImpl) and navigate there (CTRL-N and type class name), click "Download sources".
    3. Find usages of DriverManagerConnectionProviderImpl (ALT+F7)
    4. Voila! Turns out there is only one usage of DriverManagerConnectionProviderImpl - it is a initiateService(...) method in ConnectionProviderInitiator, which - more or less - tries a couple ConnectionProvider implementations, before it fallbacks to DriverManagerConnectionProviderImpl, which in our case doesn't work and throws UnknownUnwrapTypeException.

    From Hibernate's ConnectionProviderInitiator.java:

            ConnectionProvider connectionProvider = null;
        String providerClassName = getConfiguredConnectionProviderName( configurationValues );
        if ( providerClassName != null ) {
            connectionProvider = instantiateExplicitConnectionProvider( providerClassName, classLoaderService );
        }
        else if ( configurationValues.get( Environment.DATASOURCE ) != null ) {
            connectionProvider = new DatasourceConnectionProviderImpl();
        }
    
        if ( connectionProvider == null ) {
            if ( c3p0ConfigDefined( configurationValues ) && c3p0ProviderPresent( classLoaderService ) ) {
                connectionProvider = instantiateExplicitConnectionProvider( C3P0_PROVIDER_CLASS_NAME,
                        classLoaderService
                );
            }
        }
    
        if ( connectionProvider == null ) {
            if ( proxoolConfigDefined( configurationValues ) && proxoolProviderPresent( classLoaderService ) ) {
                connectionProvider = instantiateExplicitConnectionProvider( PROXOOL_PROVIDER_CLASS_NAME,
                        classLoaderService
                );
            }
        }
    
        if ( connectionProvider == null ) {
            if ( configurationValues.get( Environment.URL ) != null ) {
                connectionProvider = new DriverManagerConnectionProviderImpl();
            }
        }
    
        if ( connectionProvider == null ) {
            LOG.noAppropriateConnectionProvider();
            connectionProvider = new UserSuppliedConnectionProviderImpl();
        }
    

    The code roughly shows that:

    1. If Environment.DATASOURCE (hibernate.connection.datasource) is set, then DatasourceConnectionProviderImpl is used;
    2. If there is c3p0 present, then C3P0 Provider is instantiated
    3. Some other provider is checked (PROXOOL_PROVIDER_CLASS_NAME)
    4. Then if Environment.URL (hibernate.connection.url) is present, we use DriverManagerConnectionProviderImpl.

    And that's it. This explains why if we add C3P0 as Maven Dependency or define DataSource (as Michael ako Tecourt says in comment below) solves the issue.

    Another matter is why DriverManagerConnectionProviderImpl throws UnknownUnwrapTypeException - wheather it is a bug or not - anyway, I don't see any changes in code in Hibernate 4.2.0 and even 4.3.0.Beta.

    0 讨论(0)
  • 2021-02-05 18:49

    try include spring and hibernate library to lib folder to your project. I hope these helps.

    0 讨论(0)
  • 2021-02-05 18:55

    This article has detailed instruction on Spring 3.1 and Hibernate 4.1 integration:

    http://blog.springsource.org/2012/04/06/migrating-to-spring-3-1-and-hibernate-4-1/

    0 讨论(0)
  • 2021-02-05 18:57

    Just looking at the issue it's coming from this code:

    if ( ConnectionProvider.class.equals( unwrapType ) ||
                    DriverManagerConnectionProviderImpl.class.isAssignableFrom( unwrapType ) ) {
                return (T) this;
            }
            else {
                throw new UnknownUnwrapTypeException( unwrapType );
            }
    

    where unwrapType is javax.sql.DataSource.

    Whilst I can't explain exactly what it is about your config that causes this I can suggest a fix.

    Define a DriverManagerDataSource bean in your spring application context and then provide this as a property to your LocalSessionFactoryBean. Make sure you remove the datasource configuration from your Hibernate configuration file.

    0 讨论(0)
提交回复
热议问题