konto usunięte
Temat: Cache Spring vs Cache Hibernate , Transactional
Witam, nie mam dużej wiedzy jeśli chodzi o Springa.Mam kilka pytań
1 - W jaki sposób najlepiej cachować zapytania ? Uzywać do tego co nam daje Spring - @Cacheable and @CacheEvict, czy 2nd Level Hibernate?
2 - Co do transakcji
Jeśli mam metodę, która nie modyfikuje żadnych danyh w BD, jedynie pobiera je , musi być ona w transakcji ?
3 - Ja używam w warstwie Service adnotacji - @Transactional, @Cacheable - czy lepiej przenieść je do DAO ?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
">
<context:annotation-config />
<tx:annotation-driven />
<cache:annotation-driven/>
<context:component-scan base-package="com">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/Test" />
<property name="username" value="postgres" />
<property name="password" value="blablavla" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.max_fetch_depth">5</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses">
<array>
<value>com.model.User</value>
<value>com.model.TableRestaurant</value>
<value>com.model.Reservation</value>
</array>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<!-- Definicja magazynu-->
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<property name="name" value="tables"/>
</bean>
</set>
</property>
</bean>
</beans>
@Service
public class TableRestaurantServiceImpl implements TableRestaurantService {
@Autowired
private TableRestaurantDAO tableDAO;
@Override
@Transactional
@Cacheable("tables")
public List<TableRestaurant> listTables() {
// TODO Auto-generated method stub
return tableDAO.listTables();
}
@Override
@Transactional
@Cacheable("tables")
public List<TableRestaurant> avaibleTables() {
// TODO Auto-generated method stub
return tableDAO.avaibleTables();
}
@Override
@Transactional
public void bookTable(Long id) {
tableDAO.bookTable(id);
}
@Override
@Transactional
public void releaseTable(Long id) {
tableDAO.releaseTable(id);
}
}