Spring Access to Oracle database configuration steps

Source: Internet
Author: User

1.spring Support for database access

When we develop a persistence layer, we face a variety of options, such as using JDBC, Hibernate, the Java Persistence API, or other persistence frameworks. Fortunately, Spring is able to support all of these persistence mechanisms.

DAO (data Access Boject) is a name that describes the role that DAO plays in the application. DAO provides a way for data to be read, written to the database. They should publish the functionality as an interface, and the rest of the application can be accessed through the interface.


2. Configure the data source

Spring provides several ways to configure the data source Bean in the spring context, including:

A. Data sources defined through the JDBC driver;

B. Data sources that are found through Jndi;

C. The data source of the connection pool;

And then we'll talk about the data source that gets the connection from using the connection pool! (i.e. C point)

The code for the context configuration bean:

<span style= "FONT-SIZE:18PX;" ><!--Configuring the data source--><bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" ><property Name= "Driverclassname" value= "Oracle.jdbc.driver.OracleDriver"/><property name= "url" value= "jdbc:oracle: Thin: @localhost: 1521:orcl "/><property name=" username "value=" wwo "/><property name=" password "value=" Wwo "/><!--The initial value of the connection pool startup--><property name=" InitialSize "value=" 3 "/><!--the maximum value of the connection pool--><property Name= "Maxactive" value= "a"/><!--the maximum idle value. After a peak time, the connection pool can slowly release a portion of the connection that has not been used, until it has been reduced to maxidle-->< Property Name= "Maxidle" value= "2"/><!--minimum idle value. When the number of idle connections is less than the threshold, the connection pool will pre-apply for some connections to avoid the flood peak time to apply for--><property name= "Minidle" value= "1"/><!--end--></bean></span>

Note: The JDBC-driven data source does not have a pool concept, so there is no configuration for pool properties!
Well, at this point, we've completed the connection to the database through the data source, and the next step is to actually access the database.

3. Integrating hibernate in spring

Hibernate some features:

A. Lazy loading: For example, a Bean object is composed of its properties and another bean object, if we only focus on the properties of this bean object, then we can rely on lazy loading, only fetch the required data;

B. Pre-fetch (Eager fetching): This is relative to lazy loading, and the other beans associated with a bean will be queried, which saves the cost of multiple queries;

C. Cascade (cascading): Sometimes after you delete a Bean object, you also want it to be able to delete other beans associated with it in the database at the same time.

Spring's support for Hibernate ORM frameworks provides integration points with these frameworks as well as some additional services, as follows:

A.spring Integrated support for declarative transactions;

B. Transparent exception handling;

C. Thread-safe, lightweight template classes;

D.dao support class;

E. Resource management.

4. Declaring the session factory (session Factory)

The primary interface for using Hibernate is org.hibernate.Session. Session provides basic data access capabilities, such as saving, updating, deleting, and loading objects from a database.

With the help of Hibernate sessionfactory to get the session object, Sessionfactory is mainly responsible for opening, closing and managing hibernate session.

The beans configured in the XML context are as follows:

<span style= "FONT-SIZE:18PX;" ><bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" >< Property Name= "DataSource" ref= "DataSource"/><property name= "Packagestoscan" ><!--Scan the entity directory-->< List><value>com.cd.entity</value></list></property><property name= " Hibernateproperties "><props><prop key=" Hibernate.dialect ">org.hibernate.dialect.oracle10gdialect </prop><prop key= "Hibernate.show_sql" >true</prop><prop key= "Hibernate.format_sql" > True </prop><prop key= "Current_session_context_class" >thread</prop></props></property> </bean></span>
5. Create your own base DAO class

Spring can use the spring Hibernate template to ensure that each transaction uses the same session. Since Hibernate is able to manage itself, it is not necessary to use a template class. We then assemble the hibernate session directly into our own DAO class.

<span style= "FONT-SIZE:18PX;" >/** * Basic DAO * @param <T> */public abstract class Basedao<t> {/** * inject sessionfactory */@Autowiredprivate Se Ssionfactory sessionfactory;/** * Get session * * @return */protected session getcurrentsession () {return sessionfactory.ge Tcurrentsession ();} /** * Save * * @param entity * @throws Exception */public void Add (T entity) throws Exception {getcurrentsession (). Save (ENT ity);} /** * Call Stored procedure * * @param proname * @return */public callablestatement citepro (final String Proname) {Session session = GetC Urrentsession ();                    CallableStatement proc=session.doreturningwork (New returningwork<callablestatement> () {                    @Override public CallableStatement Execute (Connection Connection) throws sqlexception{                        CallableStatement ResultSet = Connection.preparecall (proname);                    return resultSet; }}); return proc;}} </span>



Spring Access to Oracle database configuration steps

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.