5.2. Setting Up Data Access
Spring applications either deploy their own data access infrastructure or rely on a managed infrastructure provided by the application server. For JBoss Enterprise Application Platform, Red Hat recommends you to use the infrastructure provided by the server or use a managed infrastructure using JTA transaction management.
5.2.1. Database Access Through Managed Datasources
On JBoss Enterprise Application Platform, you can configure managed datasources through the datasource subsystem and access it through JNDI. You can create a managed datasource by editing the
EAP_HOME/standalone/configuration/standalone.xml configuration file either from the CLI or from the web console.
For example, when running in the standalone mode, you can add a datasource definition by including the following element inside the
datasources subsystem.
Example 5.1. Managed Datasource Configuration in standalone.xml
<datasource jndi-name="java:jboss/datasources/ExampleDsJndiName" pool-name="ExampleDS" enabled="true"> <connection-url>jdbc:h2:mem:exampleDS</connection-url> <driver>h2</driver> <security> <user-name>sa</user-name> </security> </datasource>
The datasource is bound in JNDI at
java:jboss/datasources/ExampleDsJndiName. You can reference the datasource from a Spring ApplicationContext by using the definition in the example that follows. You can inject a datasource bean into any regular Spring bean (for example, a JBDS DAO).
Example 5.2. Defining a Managed Datasource Spring Bean
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/ExampleDsJndiName" expected-type="javax.sql.DataSource"/>
5.2.2. Hibernate SessionFactory
Applications that use Hibernate directly (as opposed to JPA) either use the Hibernate version included in the application server (for Spring versions which are compatible with Hibernate 4 and higher) by referencing it, or package a supported version of Hibernate 3 (for Spring versions that require Hibernate 3).
Spring applications can use one of Spring's SessionFactory-instantiating FactoryBeans, and a managed datasource to use Hibernate.
Example 5.3. SessionFactory Bean Definition
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="datasource"/> ... </bean>
Red Hat recommends JTA transaction management and Hibernate-JTA session management integration. You can use these by setting the following properties:
Example 5.4. JTA session management setup properties with Hibernate
hibernate.current_session_context_class=jta hibernate.transaction.jta.platform=org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
Once you define a SessionFactory, you can inject it in component classes such as DAOs and use it directly as in Example 5.5, “Hibernate-based DAO: a SessionFactory is Injected Directly in the Bean”:
Example 5.5. Hibernate-based DAO: a SessionFactory is Injected Directly in the Bean
public class HibernateAccountDao { @Autowired SessionFactory sessionFactory; public List<Account> getAllAccounts() { return sessionFactory.getCurrentSession().getCriteria(Account.class).list(); } ... }
The example is available in the Red Hat JBoss Web Framework Kit distribution, and the detailed instructions about using the example are available in the Snowdrop Sportsclub Example User Guide.