Ver Mensaje Individual
  #9 (permalink)  
Antiguo 20/04/2016, 03:57
Mariadelasflores
 
Fecha de Ingreso: marzo-2016
Mensajes: 11
Antigüedad: 8 años, 1 mes
Puntos: 0
Respuesta: Vaadin 7 + Hibernate 4 + Spring 3

Respecto a la conexión de base de datos, de momento sólo busca si existe el usuario en la base de datos y sino existe, lo crea y devuelve el objeto usuario (ahora sólo tiene el identificador). Diría que sólo se instancia una vez y luego se libera (tienes el log al final del código).

Class Provider implements AuthenticationProvider {

@Autowired
private FilterDbUtil filterDbUtil;

@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
NEWCLASSAuthenticationDetails details =
(NEWCLASSAuthenticationDetails) authentication.getDetails();
String mCustomerId = details.getCustomerId();
String mUsername = (String) authentication.getPrincipal();
String mPassword = (String) authentication.getCredentials();
>> SmpUser smpUser = filterDbUtil.getUser(mCustomerId, mUsername);
}

public class FilterDbUtil {

@Autowired
private SessionFactory sessionFactory;

public SmpUser getUser(String customerId, String username) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
String identifier = getUserIdentifier(customerId, username);
Query query = session.createQuery("from ManageUser where identifier = :identifier");
query.setParameter("identifier", identifier);
ManageUser manageUser = (ManageUser) query.uniqueResult();
tx.commit();

if (manageUser == null) {
tx = session.beginTransaction();
manageUser = new ManageUser();
manageUser.setIdentifier(identifier);
saveUser(manageUser);
tx.commit();
}
return manageUser;
} catch (HibernateException e) {
session.getTransaction().rollback();
return null;
} finally {
if (session != null){
session.close();
}
}
}
public void saveUser(SmpUser user) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.saveOrUpdate(user);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
e.printStackTrace();
}
} finally {
session.close();
}
}
}

@Entity
public class ManageUser implements BasicEntity {

@Id
@Column(name = "USER_NAME")
private String identifier;
….. //get, set, tostring
}

public interface BasicEntity extends java.io.Serializable {
}


Application.spring.xml



<!-- Data Access Objects -->
<bean id="filterDbUtil" class="………….db.FilterDbUtil" />

<!-- Hibernate settings -->
<bean id="dataSource"
class="org.apache.tomcat.dbcp.dbcp.BasicDataSource "
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.pass}" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSes sionFactoryBean">

<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="………..db.entity" />
<property name="hibernateProperties">
<props>

<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.a uto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>

<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.transaction.flush_before_completion ">true</prop>

<!-- To show sql output on the screen -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>

<!-- Connection pool C3P0 -->
<prop key="hibernate.c3p0.acquire_increment">2</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">100</prop>
<prop key="hibernate.c3p0.timeout">20</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">60</prop>

</props>
</property>
</bean>

Hibernate.properties
# jdbc.X
jdbc.driverClassName=org.apache.derby.jdbc.Embedde dDriver
jdbc.url=jdbc:derby:${catalina.home}/FilterDB;create=true
jdbc.user=
jdbc.pass=

# hibernate.X
hibernate.dialect=com.........nd.db.FixedDerbyDial ect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update


Info. Conexión base de datos:

11:16:29.105 [http-bio-8200-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtaining JDBC connection
11:16:29.105 [http-bio-8200-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Obtained JDBC connection
.... busca ... crea...
11:16:31.309 [http-bio-8200-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - committed JDBC Connection
11:16:31.310 [http-bio-8200-exec-4] DEBUG o.h.e.t.i.jdbc.JdbcTransaction - re-enabling autocommit
11:16:31.313 [http-bio-8200-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Releasing JDBC connection
11:16:31.314 [http-bio-8200-exec-4] DEBUG o.h.e.j.i.LogicalConnectionImpl - Released JDBC connection
11:16:31.315 [http-bio-8200-exec-4] DEBUG o.h.e.j.i.p.ConnectionProxyHandler - HHH000163: Logical connection releasing its physical connection

Muchas gracias!!!!