2017-05-04 5 views
-1

JPAを使用してAuthorオブジェクトを格納するSpringアプリケーションがあります。私はデータベースクラスのメソッドを記述していますので、適切な操作を保証するために、特定の「テンプレート」が使用されています。しかし、私はちょっと初心者です。これがいつも必要なのか、それとも望ましいのかは分かりません。ベストプラクティスに関するコメントや情報は大歓迎です!私はこのコードのいずれかを使用することはありませんJPAデータベースクラスのメソッド

テンプレート

openConnection(); 
    EntityTransaction transaction = this.em.getTransaction(); 
    try { 
     transaction.begin(); 
     //DO STUFF HERE 
     transaction.commit(); 
    } catch (Exception e) { 
     if(transaction.isActive()) { 
      transaction.rollback(); 
     } 
     throw new DatabaseException(e.getMessage(), e); 
    } finally { 
     closeConnection(); 
    } 

データベース全体のコード

public class AuthorDatabaseDerby implements AuthorDatabase { 

    private static volatile AuthorDatabaseDerby uniqueInstance; 
    private EntityManagerFactory emf; 
    private EntityManager em; 

    public static AuthorDatabaseDerby getInstance() { 
     if(uniqueInstance == null) { 
      synchronized(AuthorDatabaseDerby.class) { 
       if(uniqueInstance == null) { 
        uniqueInstance = new AuthorDatabaseDerby(); 
       } 
      } 
     } 
     return uniqueInstance; 
    } 

    private AuthorDatabaseDerby() { 
     this.emf = Persistence.createEntityManagerFactory("bookstore"); 
    } 

    private void openConnection() { 
     this.em = this.emf.createEntityManager(); 
    } 

    private void closeConnection() throws DatabaseException { 
     try { 
      if(this.em != null) { 
       this.em.close(); 
      } 
     } catch(Exception e) { 
      throw new DatabaseException(e.getMessage(), e); 
     } 
    } 

    @Override 
    public Author get(int id) throws DatabaseException { 
     openConnection(); 
     EntityTransaction transaction = this.em.getTransaction(); 
     try { 
      transaction.begin(); 
      Author author = this.em.find(Author.class, id); 
      transaction.commit(); 
      return author; 
     } catch (Exception e) { 
      if(transaction.isActive()) { 
       transaction.rollback(); 
      } 
      throw new DatabaseException(e.getMessage(), e); 
     } finally { 
      closeConnection(); 
     } 
    } 

    @Override 
    public List<Author> getAll() throws DatabaseException { 
     openConnection(); 
     EntityTransaction transaction = this.em.getTransaction(); 
     try { 
      transaction.begin(); 
      List<Author> authors = this.em.createQuery("Select a From Author a", Author.class).getResultList(); 
      transaction.commit(); 
      return authors; 
     } catch(Exception e) { 
      if(transaction.isActive()) { 
       transaction.rollback(); 
      } 
      throw new DatabaseException(e.getMessage(), e); 
     } finally { 
      closeConnection(); 
     } 
    } 

    @Override 
    public void add(Author author) throws DatabaseException { 
     openConnection(); 
     EntityTransaction transaction = this.em.getTransaction(); 
     try { 
      transaction.begin(); 
      this.em.persist(author); 
      transaction.commit(); 
     } catch(Exception e) { 
      if(transaction.isActive()) { 
       transaction.rollback(); 
      } 
      throw new DatabaseException(e.getMessage(), e); 
     } finally { 
      closeConnection(); 
     } 
    } 

    @Override 
    public void update(Author author) throws DatabaseException { 
     openConnection(); 
     EntityTransaction transaction = this.em.getTransaction(); 
     try { 
      transaction.begin(); 

      Author a = this.em.find(Author.class, author.getId()); 
      a.setBooks(author.getBooks()); 
      a.setDateBirth(author.getDateBirth()); 
      a.setDateDeceased(author.getDateDeceased()); 
      a.setFirstName(author.getFirstName()); 
      a.setId(author.getId()); 
      a.setLastName(author.getLastName()); 
      a.setNationality(author.getNationality()); 

      transaction.commit(); 
     } catch(Exception e) { 
      if(transaction.isActive()) { 
       transaction.rollback(); 
      } 
      throw new DatabaseException(e.getMessage(), e); 
     } finally { 
      closeConnection(); 
     } 
    } 

    @Override 
    public void delete(int id) throws DatabaseException { 
     openConnection(); 
     EntityTransaction transaction = this.em.getTransaction(); 
     try { 
      transaction.begin(); 
      Author author = this.em.find(Author.class, id); 
      this.em.remove(author); 
      transaction.commit(); 
     } catch(Exception e) { 
      if(transaction.isActive()) { 
       transaction.rollback(); 
      } 
      throw new DatabaseException(e.getMessage(), e); 
     } finally { 
      closeConnection(); 
     } 
    } 

    @Override 
    public void close() throws DatabaseException { 
     try { 
      if(this.emf != null) { 
       this.emf.close(); 
      } 
     } catch(Exception e) { 
      throw new DatabaseException(e.getMessage(), e); 
     } 
    } 

} 
+0

あなたはアドバイスやアドバイスではなく、承認を求めているようですね。 – duffymo

答えて

1

私はあなたのテンプレートにSpringトランザクション管理を使いたいと思います。アノテーションと設定に基づいています。

接続クラスではなく接続プールを使用します。

Springから既に入手可能なものを使用できるときにコードを書く理由は何ですか?彼らはあなたや私よりも優れたコードを書いています。

+0

ありがとうございます。しかし、これは学校の課題で、私はビジネスロジックをフレームワークから切り離すことを前提としているので、Springのトランザクション管理は使用できません。 –

+0

OK。もちろん、ここで読んだことは何でも自由に無視できます。あなたはベストプラクティスを求めました。コードは1つではありません。プールはありません。これを、あなたがエミュレートできるより良い例として考えるかもしれません。あなたはSpringのソースコードを見て、彼らがそのデザインについてどのように考えているかを自由に知ることができます。 – duffymo

関連する問題