2017-05-04 70 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

相关问题