2017-05-04 63 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已經提供的代碼?他們編寫的代碼比你或我的編碼更好。有更廣泛的用戶受衆可以找到缺陷。

+0

謝謝你的回答。但是,這是一個學校任務,我願意將我的業務邏輯從框架中分離出來,所以我不能使用Spring事務管理。 –

+0

好的。當然,你可以自由地忽略你在這裏閱讀的任何內容。你要求最佳實踐 - 你的代碼不是一個。沒有池。你可以把它看作是你可以效仿的一個更好的例子。您可以自由查看Spring源代碼並查看他們對設計的看法。 – duffymo