Hi.
I'm new using hibernate, spring and struts.
I have a small aplication that use all this 3 frameworks but know i need to implement this using database pooling.
To do so i change my aplicationContext.xml like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- datasource (database connection) -->
<!-- poolable connection -->
<bean id="poolableConnectionFactory" class="org.apache.commons.dbcp.PoolableConnectionF actory">
<constructor-arg index="0"><ref bean="dsConnectionFactory"/></constructor-arg>
<constructor-arg index="1"><ref bean="pool"/></constructor-arg>
<constructor-arg index="2"><null/></constructor-arg>
<constructor-arg index="3"><null/></constructor-arg>
<constructor-arg index="4"><value>false</value></constructor-arg>
<constructor-arg index="5"><value>true</value></constructor-arg>
</bean>
<bean id="dsConnectionFactory" class="org.apache.commons.dbcp.DataSourceConnectio nFactory">
<constructor-arg><ref bean="cpmsDS"/></constructor-arg>
</bean>
<bean id="pool" class="org.apache.commons.pool.impl.GenericObjectP ool">
<property name="minEvictableIdleTimeMillis"><value>300000</value></property>
<property name="timeBetweenEvictionRunsMillis"><value>60000</value></property>
</bean>
<bean id="cpmsDS" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
<property name="driverClassName"><value>com.mysql.jdbc.Drive r</value></property>
<property name="url"><value>jdbc:mysql://localhost:3306/bi</value></property>
<property name="username"><value>root</value></property>
<property name="password"><value>1234</value></property>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="pooledDS" class="org.apache.commons.dbcp.PoolingDataSource" depends-on="poolableConnectionFactory">
<constructor-arg><ref bean="pool"/></constructor-arg>
</bean>
<!-- References all OR mapping files. -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
<property name="dataSource"><ref local="pooledDS"/></property>
<property name="mappingResources">
<list>
<value>form/action/Person.hbm.xml</value>
</list>
</property>
<!-- Set the type of database; changing this one property will port this to Oracle, MS SQL etc. -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQ LDialect</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<!-- parent template for the transaction proxy beans -->
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- personMangerDao data access object: Hibernate implementation -->
<bean id="personDAO" class="form.dao.hibernate.PersonDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<!-- Transactional proxy for jobManagerDao data access object. -->
<bean id="personManagerDAO" parent="txProxyTemplate">
<property name="target"><ref local="personDAO"/></property>
</bean>
<bean id="personAction" class="form.action.PersonAction">
<constructor-arg ref="personDAO"/>
</bean>
</beans>
The personAction.java it's here:
package form.action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import form.action.Person;
import form.dao.hibernate.PersonDAO;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class PersonAction extends ActionSupport {
List persons;
PersonDAO service;
Person person;
private int id;
String message;
public String execute(){
this.persons = service.getPersons();
return SUCCESS;
}
public String save() {
if (person.getAge() != null && person.getFirstName() != null
&& person.getLastName() != null) {
person.setAgeDays(Integer.parseInt(person.getAge() ) * 365);
System.out.println("SAVE PERSON");
this.service.savePerson(person);
//this.setMessage("Sucesso! Esta mensagem foi gerada na Action");
return execute() ;
} else {
this.setMessage("Erro! Esta mensagem foi gerada na Action");
return ERROR;
}
}
public String remove(){
this.service.removePerson(id);
return execute();
}
public String edit(){
setPerson(service.getPerson(id));
return SUCCESS;
}
public String update(){
if (person.getAge() != null && person.getFirstName() != null && person.getLastName() != null)
person.setAgeDays(Integer.parseInt(person.getAge() ) * 365);
person.setId(person.getId());
System.out.println(person.id);
System.out.println(person.firstName);
System.out.println(person.lastName);
System.out.println(person.age);
System.out.println(person.ageDays);
service.updatePerson(person);
return execute();
}
public PersonAction(PersonDAO service){
this.service = service;
}
/************************************************** *************************/
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public List<Person> getPersons() {
return persons;
}
public void setPersons(List persons) {
this.persons = persons;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
I had to the web.xml this lines, because i create a MySQL pooling in the Websphere Comunity Edition:
<resource-ref>
<res-ref-name>jdbc/MyDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
If you need more information please let me know.
I need help fast as possible....
Thx.


Reply With Quote
