代码之家  ›  专栏  ›  技术社区  ›  Gabriel Sansigolo

SpingREST:无法为事务打开JPA EntityManager;嵌套异常为org。冬眠

  •  0
  • Gabriel Sansigolo  · 技术社区  · 7 年前

    SpringREST 服务正在运行时,我收到以下错误:

    {
        "timestamp": 1506965117328,
        "status": 500,
        "error": "Internal Server Error",
        "exception": "org.springframework.transaction.CannotCreateTransactionException",
        "message": "Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection",
        "path": "/api/matchs"
    }
    

    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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.kaluzny</groupId>
        <artifactId>spring-boot-rest-api-postgresql</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.3.RELEASE</version>
            <relativePath/>
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-rest</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
            <!-- PostgreSQL-->
            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>9.4-1201-jdbc41</version>
                <scope>runtime</scope>
            </dependency>
            <!-- Java EE -->
            <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    应用。Java语言

    package com.kaluzny;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    MatchRestController。Java语言

    package com.kaluzny.web;
    
    import com.kaluzny.domain.*;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;
    
    import javax.inject.Inject;
    import java.util.Collection;
    
    @RestController
    @RequestMapping("/api/matchs")
    public class MatchRestController {
    
        private MatchRepository repository;
    
        @Inject
        public void setRepository(MatchRepository repository) {
            this.repository = repository;
        }
    
        @RequestMapping(
                method = RequestMethod.POST)
        public ResponseEntity<?> addMatch(@RequestBody Match match) {
            return new ResponseEntity<>(repository.save(match), HttpStatus.CREATED);
        }
    
        @RequestMapping(
                method = RequestMethod.GET)
        public ResponseEntity<Collection<Match>> getAllMatch() {
            return new ResponseEntity<>(repository.findAll(), HttpStatus.OK);
        }
    
        @RequestMapping(
                value = "/{idMatch}",
                method = RequestMethod.GET)
        public ResponseEntity<Match> getMatchWithIdMatch(@PathVariable Integer idMatch) {
            return new ResponseEntity<>(repository.findOne(idMatch), HttpStatus.OK);
        }
    
        @RequestMapping(
                value = "/{idMatch}",
                method = RequestMethod.PUT)
        public ResponseEntity<Match> updateMatchFromDB(@PathVariable("idMatch") Integer idMatch, @RequestBody Match match) {
    
            Match currentMatch = repository.findOne(idMatch);
            currentMatch.setIdMatch(match.getIdMatch());
            currentMatch.setNumberPlayers(match.getNumberPlayers());
            currentMatch.setWinner(match.getWinner());
            currentMatch.setScore(match.getScore());
            currentMatch.setNumberSpike(match.getNumberSpike());
            currentMatch.setNumberFireball(match.getNumberFireball());
            currentMatch.setNumberNuke(match.getNumberNuke());
    
            Match currentMatch1 = repository.findOne(idMatch);
    
    
    
            return new ResponseEntity<>(repository.save(currentMatch1), HttpStatus.OK);
        }
    
        @RequestMapping(
                value = "/{idMatch}",
                method = RequestMethod.DELETE)
        public void deleteMatchWithId(@PathVariable Integer idMatch) {
            repository.delete(idMatch);
        }
    
        @RequestMapping(
                method = RequestMethod.DELETE)
        public void deleteAllMatchs() {
            repository.deleteAll();
        }
    }
    

    匹配存储库。Java语言

    package com.kaluzny.domain;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.rest.core.annotation.RepositoryRestResource;
    
    import java.util.List;
    
    @RepositoryRestResource
    public interface MatchRepository extends JpaRepository<Match, Integer> {
        List<Match> findByIdMatch(Integer idMatch);
    }
    

    火柴Java语言

    package com.kaluzny.domain;
    
    import javax.persistence.*;
    
    @Entity
    public class Match {
    
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
    
        private Integer idMatch;
        private Integer numberPlayers;
        private String winner;
        private Integer score;
        private Integer numberSpike;
        private Integer numberFireball;
        private Integer numberNuke;
    
    
        public Match(Integer idMatch, Integer numberPlayers, String winner, Integer score, Integer numberSpike, Integer numberFireball, Integer numberNuke) {
            this.idMatch = idMatch;
            this.numberPlayers = numberPlayers;
            this.winner = winner;
            this.score = score;
            this.numberSpike = numberSpike;
            this.numberFireball = numberFireball;
            this.numberNuke = numberNuke;
        }
    
        public Match() {
        }
    
        public void setIdMatch(Integer idMatch){
            this.idMatch = idMatch;
        }
    
        public Integer getIdMatch() {
            return idMatch;
        }
    
        public void setNumberPlayers(Integer numberPlayers){
            this.numberPlayers = numberPlayers;
        }
    
        public Integer getNumberPlayers(){
            return numberPlayers;
        }
    
        public void setWinner(String winner){
            this.winner = winner;
        }
    
        public String getWinner(){
            return winner;
        }
    
        public void setScore(Integer score){
            this.score = score;
        }
    
        public Integer getScore(){
            return score;
        }
    
        public void setNumberSpike(Integer numberSpike){
            this.numberSpike = numberSpike;
        }
    
        public Integer getNumberSpike(){
            return numberSpike;
        }
    
        public void setNumberFireball(Integer numberFireball){
            this.numberFireball = numberFireball;
        }
    
        public Integer getNumberFireball(){
            return numberFireball;
        }
    
        public void setNumberNuke(Integer numberNuke){
            this.numberNuke = numberNuke;
        }
    
        public Integer getNumberNuke(){
            return numberNuke;
        }
    
    
        @Override
        public String toString() {
            return "Partida{" +
                "Indentificador Partida: " + numberPlayers + '\'' +
                "Numero Jogadores: " + numberPlayers + '\'' +
                "Vencedor: "+ winner  + '\'' +
                "Pontuação: " + score + '\'' +
                "Numero de Espinhos: " + numberSpike + '\'' +
                "Numero de Fireball" + numberFireball + '\'' +
                "Numero de Nukes" + numberNuke + '}';
        }
    }
    

    谢谢大家。

    1 回复  |  直到 7 年前
        1
  •  -3
  •   Madhu Reddy    7 年前

    很明显,似乎存在一个连接问题。请检查您的申请。属性文件,并查看数据库配置是否正确。