I want to make sure that since I am using @PersistenceContext I do not need to close any connections so as to avoid leakages and any left open connections and poor performance. So my applicationContext.xml looks as follows (where i define the entitymanager factory etc..)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<context:component-scan base-package="com.companyname.*" />
<tx:annotation-driven/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:com/urbanbuz/controller/persistence.xml" />
<property name="persistenceUnitName" value="userPersistenceUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="showSql" value="true"/>
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="dataSource" ref="dataSource" />
<property name="jpaDialect" ref="jpaDialect" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ub" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
My persistence xml is as follows accordingly:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="userPersistenceUnit" transaction-type="RESOURCE_LOCAL" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.urbanbuz.model.User</class>
<class>com.urbanbuz.model.Account</class>
<class>com.urbanbuz.model.AccountDetails</class>
<class>com.urbanbuz.model.Posting</class>
<class>com.urbanbuz.model.Journal</class>
</persistence-unit>
Now for each of those models I have a DAO and Service class, as an example I am providing one:
@Repository("accountDao")
@Transactional(propagation = Propagation.REQUIRED)
public class AccountDAO {
@PersistenceContext
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
// method inserts account into database
public void insert(Account account) {
entityManager.persist(account);
}
}
@Service
public class AccountService {
private AccountDAO accountDAO;
public AccountDAO getAccountDao() {
return accountDAO;
}
@Autowired
public void setAccountDao(AccountDAO accountDAO) {
this.accountDAO = accountDAO;
}
public void addAccount(Account account) {
getAccountDao().insert(account);
}
}
So whenever I need to access the database and perform any actions I define the following: ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml") and then define the context EntityService entityService = (EntityService) context.getBean("entityService") and accordingly call the needed methods. Do I need any further special management?
Edit: App.Java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class App {
public static void main(String[] args) {
// here i just initialize an instance of the a component I have
SignupComponent sc = new SignupComponent();
// some code
sc.signUp();
}
}
In the component I am trying to autowire the entities as such:
public class SignupComponent {
@Autowired
EntityService entityService;
//using it as follows for example: entityService.getEntity(entity_id);
}