我的项目中的Spring MVC PostgreSQL错误:ResourcePool无法从其主要工厂或源获取资源

时间:2016-08-25 13:32:03

标签: java spring postgresql hibernate maven

当我尝试将我的spring项目连接到Postgre时,我发现了以下错误: 我已经检查了我的密码及其管理员。 接下来是很多教程,在stackoverflow上看到了一些问题,但没有任何效果。

任何人都可以帮助我吗?

在我使用mysql之前,它运行得很好。

        Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection
            at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
            at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
            at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1397)
            at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:62)
            at org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction(DefaultJpaDialect.java:71)
            at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:61)
            at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:378)
            ... 50 more
        Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
            at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
            at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
            at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
            at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)
            at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
            at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
            at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
            at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)
            at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:59)
            ... 53 more
        Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
            at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
            at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
            at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
            at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:70)
            at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
            at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
            ... 58 more
        Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
            at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
            at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
            at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
            at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)

这是我的hibernate-context.xml:

            <?xml version="1.0" encoding="UTF-8"?>
            <beans 
                xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:context="http://www.springframework.org/schema/context"
                xmlns:tx="http://www.springframework.org/schema/tx"
                xmlns:jpa="http://www.springframework.org/schema/data/jpa"
                xmlns:mvc="http://www.springframework.org/schema/mvc"
                xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

                <context:property-placeholder location="classpath:jdbc.properties" />

                <bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
                    <property name="targetClass"><value>java.lang.System</value></property>
                    <property name="targetMethod"><value>setProperty</value></property>
                    <property name="arguments">
                        <list>
                            <value>com.mchange.v2.c3p0.management.ManagementCoordinator</value>
                            <value>com.mchange.v2.c3p0.management.NullManagementCoordinator</value>
                        </list>
                    </property>
                </bean>

                <bean id="appDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" depends-on="sysProps">
                    <property name="driverClass" value="${jdbc.driverClassName}"/>
                    <property name="jdbcUrl" value="${jdbc.url}"/>
                    <property name="user" value="${jdbc.username}"/>        
                    <property name="password" value="${jdbc.password}"/>

                    <!-- these are C3P0 properties --> 
                    <!-- 
                    <property name="acquireIncrement" value="${c3p0.acquireIncrement}" /> 
                    <property name="minPoolSize" value="${c3p0.minPoolSize}" /> 
                    <property name="maxPoolSize" value="${c3p0.maxPoolSize}" /> 
                    <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />    
                    <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" /> 
                    <property name="preferredTestQuery" value="${c3p0.preferredTestQuery}" /> 
                     -->
                </bean>

                <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                    <property name="dataSource" ref="appDataSource" />
                    <property name="persistenceUnitName" value="websysPU" />
                    <property name="jpaVendorAdapter">
                        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
                    </property>
                </bean> 

                <bean id="transactionManager" 
                      class="org.springframework.orm.jpa.JpaTransactionManager">
                  <property name="entityManagerFactory" ref="entityManagerFactory"/>
                </bean>

                <bean id="multipartResolver"
                    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

                    <!-- one of the properties available; the maximum file size in bytes -->
                    <property name="maxUploadSize" value="10000000" />
                </bean>

                <tx:annotation-driven/>

                <jpa:repositories base-package="br.com.system" />

            </beans>

jdbc.properties:

            jdbc.driverClassName=com.postgresql.Driver
            jdbc.url=jdbc:postgresql://localhost:5433/postgres
            jdbc.username=postgres
            jdbc.password=admin 

的persistence.xml:

            <persistence 
                version="2.0"
                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">

                <persistence-unit name="websysPU" transaction-type="RESOURCE_LOCAL">

                    <!-- provedor do JPA -->
                    <provider>org.hibernate.ejb.HibernatePersistence</provider>

                    <properties>
                        <!--  opa -->
                        <!-- propriedades do hibernate -->
                        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
                        <property name="hibernate.show_sql" value="true" />
                        <property name="hibernate.format_sql" value="false" />

                        <property name="connection.provider_class" value="org.springframework.jdbc.datasource.DriverManagerDataSource" />

                        <!-- atualiza o banco, gera as tabelas se for preciso -->
                        <property name="hibernate.hbm2ddl.auto" value="create" /> <!-- Alterar para create sempre que precisar alterar o schema -->
                        <property name="hibernate.hbm2ddl.import_files" value="initial_data.sql" /> 

                    </properties>

                </persistence-unit>

            </persistence>

和pom.xml:

            <?xml version="1.0" encoding="UTF-8"?>
            <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

                <modelVersion>4.0.0</modelVersion>
                <groupId>br.com.system</groupId>
                <artifactId>websys</artifactId>
                <name>WebSys System</name>
                <packaging>war</packaging>
                <version>1.0.0.1</version>

                <properties>
                    <java-version>1.7</java-version>
                    <org.springframework-version>3.2.3.RELEASE</org.springframework-version>
                    <org.springsecurity-version>3.1.4.RELEASE</org.springsecurity-version>
                    <org.aspectj-version>1.6.10</org.aspectj-version>
                    <org.slf4j-version>1.5.6</org.slf4j-version>
                </properties>

                <dependencies>


                    <!-- Spring 3 -->

                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                        <version>${org.springframework-version}</version>
                    </dependency>

                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                        <version>${org.springframework-version}</version>
                    </dependency>

                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context-support</artifactId>
                        <version>${org.springframework-version}</version>
                        <exclusions>
                            <exclusion>
                                <artifactId>spring-context</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                        </exclusions>
                    </dependency>

                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-web</artifactId>
                        <version>${org.springframework-version}</version>
                        <exclusions>
                            <exclusion>
                                <artifactId>spring-context</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                        </exclusions>
                    </dependency>

                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-webmvc</artifactId>
                        <version>${org.springframework-version}</version>
                        <exclusions>
                            <exclusion>
                                <artifactId>spring-context</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                        </exclusions>
                    </dependency>


                    <!-- sitemesh -->
                     <dependency>
                        <groupId>opensymphony</groupId>
                        <artifactId>sitemesh</artifactId>
                        <version>2.4.2</version>
                    </dependency>

                    <!-- Spring Security -->
                    <dependency>
                        <groupId>org.springframework.security</groupId>
                        <artifactId>spring-security-core</artifactId>
                        <version>${org.springsecurity-version}</version>
                        <exclusions>
                            <exclusion>
                                <artifactId>spring-context</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                        </exclusions>
                    </dependency>

                    <dependency>
                        <groupId>org.springframework.security</groupId>
                        <artifactId>spring-security-web</artifactId>
                        <version>${org.springsecurity-version}</version>
                        <exclusions>
                            <exclusion>
                                <artifactId>spring-tx</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                            <exclusion>
                                <artifactId>spring-context</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                        </exclusions>
                    </dependency>

                    <dependency>
                        <groupId>org.springframework.security</groupId>
                        <artifactId>spring-security-config</artifactId>
                        <version>${org.springsecurity-version}</version>
                        <exclusions>
                            <exclusion>
                                <artifactId>spring-context</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                        </exclusions>
                    </dependency>   

                    <dependency>
                        <groupId>org.springframework.security</groupId>
                        <artifactId>spring-security-taglibs</artifactId>
                        <version>${org.springsecurity-version}</version>
                        <exclusions>
                            <exclusion>
                                <artifactId>spring-tx</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                            <exclusion>
                                <artifactId>spring-context</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                        </exclusions>
                    </dependency>

                    <!-- Validação -->
                    <dependency>
                        <groupId>javax.validation</groupId>
                        <artifactId>validation-api</artifactId>
                        <version>1.0.0.GA</version>
                    </dependency>

                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-validator</artifactId>
                        <version>4.0.0.GA</version>
                    </dependency>

                    <!-- Possibilita a criação de api controller rest usando JSON -->
                    <dependency>
                        <groupId>org.codehaus.jackson</groupId>
                        <artifactId>jackson-mapper-asl</artifactId>
                        <version>1.9.12</version>
                    </dependency>

                    <!-- AspectJ -->
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${org.aspectj-version}</version>
                    </dependency>   

                    <!-- Logging -->
                    <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-api</artifactId>
                        <version>${org.slf4j-version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>jcl-over-slf4j</artifactId>
                        <version>${org.slf4j-version}</version>
                        <scope>runtime</scope>
                    </dependency>
                    <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                        <version>${org.slf4j-version}</version>
                        <scope>runtime</scope>
                    </dependency>
                    <dependency>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                        <version>1.2.15</version>
                        <exclusions>
                            <exclusion>
                                <groupId>javax.mail</groupId>
                                <artifactId>mail</artifactId>
                            </exclusion>
                            <exclusion>
                                <groupId>javax.jms</groupId>
                                <artifactId>jms</artifactId>
                            </exclusion>
                            <exclusion>
                                <groupId>com.sun.jdmk</groupId>
                                <artifactId>jmxtools</artifactId>
                            </exclusion>
                            <exclusion>
                                <groupId>com.sun.jmx</groupId>
                                <artifactId>jmxri</artifactId>
                            </exclusion>
                        </exclusions>
                        <scope>runtime</scope>
                    </dependency>

                    <!-- @Inject -->
                    <dependency>
                        <groupId>javax.inject</groupId>
                        <artifactId>javax.inject</artifactId>
                        <version>1</version>
                    </dependency>

                    <!-- Servlet -->
                    <dependency>
                        <groupId>javax.servlet</groupId>
                        <artifactId>servlet-api</artifactId>
                        <version>2.5</version>
                        <scope>provided</scope>
                    </dependency>
                    <dependency>
                        <groupId>javax.servlet.jsp</groupId>
                        <artifactId>jsp-api</artifactId>
                        <version>2.1</version>
                        <scope>provided</scope>
                    </dependency>
                    <dependency>
                        <groupId>javax.servlet</groupId>
                        <artifactId>jstl</artifactId>
                        <version>1.2</version>
                    </dependency>

                    <!-- Test -->
                    <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.7</version>
                        <scope>test</scope>
                    </dependency>  

                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-test</artifactId>
                        <version>${org.springframework-version}</version>
                    </dependency>

                    <!-- Data -->
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-entitymanager</artifactId>
                        <version>4.2.2.Final</version>
                    </dependency>
                    <dependency> 
                        <groupId>com.h2database</groupId>
                        <artifactId>h2</artifactId>
                        <version>1.3.162</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.data</groupId>
                        <artifactId>spring-data-redis</artifactId>
                        <version>1.0.0.RELEASE</version>
                        <exclusions>
                            <exclusion>
                                <artifactId>spring-tx</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-orm</artifactId>
                        <version>${org.springframework-version}</version>
                        <exclusions>
                            <exclusion>
                                <artifactId>spring-tx</artifactId>
                                <groupId>org.springframework</groupId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>commons-dbcp</groupId>
                        <artifactId>commons-dbcp</artifactId>
                        <version>1.4</version>
                    </dependency>
                    <!-- 
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.17</version>
                    </dependency>
                    -->
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>9.3-1100-jdbc41</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.data</groupId>
                        <artifactId>spring-data-jpa</artifactId>
                        <version>1.3.2.RELEASE</version>
                    </dependency> 

                    <dependency>
                        <groupId>org.jboss.resteasy</groupId>
                        <artifactId>resteasy-jaxrs</artifactId>
                        <version>2.3.2.Final</version>
                    </dependency>

                    <dependency>
                        <groupId>org.jboss.resteasy</groupId>
                        <artifactId>resteasy-jackson-provider</artifactId>
                        <version>3.0.1.Final</version>
                    </dependency>

                    <dependency>
                        <groupId>c3p0</groupId>
                        <artifactId>c3p0</artifactId>
                        <version>0.9.1.2</version>
                    </dependency>

                    <!-- Jackson/Hibernate -->

                    <dependency>
                        <groupId>com.fasterxml.jackson.datatype</groupId>
                        <artifactId>jackson-datatype-hibernate4</artifactId>
                        <version>2.2.3</version>
                    </dependency>    

                    <!-- Apache Commons Upload --> 
                    <dependency>
                        <groupId>commons-fileupload</groupId>
                        <artifactId>commons-fileupload</artifactId>
                        <version>1.3.1</version>
                    </dependency>

                    <dependency>
                        <groupId>net.coobird</groupId>
                        <artifactId>thumbnailator</artifactId>
                        <version>[0.4, 0.5)</version>
                    </dependency>

                    <!-- Apache Commons IO -->
                    <dependency>
                        <groupId>commons-io</groupId>
                        <artifactId>commons-io</artifactId>
                        <version>2.4</version>
                    </dependency>


                    <!-- Java Mail API -->
                    <dependency>
                        <groupId>javax.mail</groupId>
                        <artifactId>mail</artifactId>
                        <version>1.4.3</version>
                    </dependency> 

                </dependencies>
                <build>
                    <plugins>
                        <plugin>
                            <artifactId>maven-eclipse-plugin</artifactId>
                            <version>2.9</version>
                            <configuration>
                                <additionalProjectnatures>
                                    <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                                </additionalProjectnatures>
                                <additionalBuildcommands>
                                    <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                                </additionalBuildcommands>
                                <downloadSources>true</downloadSources>
                                <downloadJavadocs>true</downloadJavadocs>
                            </configuration>
                        </plugin>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <version>2.5.1</version>
                            <configuration>
                                <source>1.6</source>
                                <target>1.6</target>
                                <compilerArgument>-Xlint:all</compilerArgument>
                                <showWarnings>true</showWarnings>
                                <showDeprecation>true</showDeprecation>
                            </configuration>
                        </plugin>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>exec-maven-plugin</artifactId>
                            <version>1.2.1</version>
                            <configuration>
                                <mainClass>org.test.int1.Main</mainClass>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
                <organization>
                    <name>System SA</name>
                </organization>
                <dependencyManagement>
                    <dependencies>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-aop</artifactId>
                            <version>3.2.3.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-beans</artifactId>
                            <version>3.2.3.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-expression</artifactId>
                            <version>3.2.3.RELEASE</version>
                        </dependency>
                        <dependency>
                            <groupId>commons-codec</groupId>
                            <artifactId>commons-codec</artifactId>
                            <version>1.4</version>
                        </dependency>
                        <dependency>
                            <groupId>commons-logging</groupId>
                            <artifactId>commons-logging</artifactId>
                            <version>1.1.1</version>
                        </dependency>
                        <dependency>
                            <groupId>org.codehaus.jackson</groupId>
                            <artifactId>jackson-core-asl</artifactId>
                            <version>1.9.12</version>
                        </dependency>
                        <dependency>
                            <groupId>org.jboss.logging</groupId>
                            <artifactId>jboss-logging</artifactId>
                            <version>3.1.0.GA</version>
                        </dependency>
                    </dependencies>
                </dependencyManagement>
            </project>

和我的Postgre道具: postgree

1 个答案:

答案 0 :(得分:0)

而不是

jdbc.driverClassName=com.postgresql.Driver

使用

jdbc.driverClassName =org.postgresql.Driver

我在我的项目中使用了这个。无法看到任何其他问题