Spring makes LOB data operations simple and easy

Source: Internet
Author: User

Overview

LOB represents large object data, including BLOB and CLOB two types, used to store large chunks of binary data, such as picture data, video data, etc., which are used to store long text data, such as forum post content, product description, and so on. It is noteworthy that: in different databases, the corresponding field types of large objects are not the same, such as DB2 corresponding blob/clob,mysql corresponding blob/longtext,sqlserver corresponding image/text. It should be noted that some databases have large object types that can be accessed like simple types, such as the way MySql Longtext and VARCHAR types. In general, LOB-type data is accessed differently than other simple types of data, and we often manipulate lob-type data in a streaming fashion. In addition, access to LOB type data is not thread-safe, and the corresponding database resources need to be allocated separately and freed after the operation completes. Finally, Oracle 9i has a very personalized use of non-JDBC standard APIs to manipulate LOB data. All of this poses challenges for writing programs that manipulate LOB type data, and Spring provides us with the appropriate help classes in the Org.springframework.jdbc.support.lob package so that we can easily handle the stumbling block.

Spring greatly reduces the difficulty of dealing with LOB data. First, Spring provides a nativejdbcextractor interface in which you can select the appropriate implementation class to fetch the local JDBC object from the data source, and then spring uses the Lobcreator interface to eliminate the difference between different data vendors manipulating LOB data. The Lobhandler interface for creating lobcreator is provided, and you can configure it by selecting the appropriate lobhandler based on the underlying database type.

This article describes in detail the process of inserting and accessing LOB data through Spring JDBC. You can easily access LOB data through both Lobcreator and Lobhandler, either in blocks or in a streaming manner. For ORM frameworks, JPA has its own configuration type to handle LOB data, and Spring provides the Hibernate and IBatis with the configuration classes for LOB data types, which you only need to use for simple configuration to manipulate LOB type data like normal types.

Local JDBC Object

When you configure a data source in a WEB application server or Spring, the data connection object returned from the data source is a proxy class for local JDBC objects, such as Db2connection, OracleConnection, This is because the data source needs to change some of the existing behavior of the data connection to control it: if you call the Connection#close () method, return the data connection to the connection pool instead of actually shutting it down.

When you access LOB data, depending on the database vendor, you may need to use APIs that are specific to the local JDBC object (such as Db2connection or Db2resultset) before the proxy. To get the local JDBC object from the data source, Spring defines the Org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor interface and provides the corresponding implementation class. NativeJdbcExtractor defines several ways to extract a local JDBC object from a data source:

/tr>
method description
get local Connection object
connection getnativeconnectionfromstatemen T (Statement stmt)
preparedstatement getnativepreparedstatement (preparedstatement PS) > get local PreparedStatement object
resultset Getnativeresultset (ResultSet RS
callablestatement getnativecallablestatement (CallableStatement cs) > get local CaLlablestatement Object

Some simple data sources broker only the Connection object, and you can use the Simplenativejdbcextractor implementation class directly. However, some data sources, such as Jakarta Commons DBCP, are proxies for all JDBC objects, and then you need to select the appropriate extractor implementation class based on the specific circumstances. The following table lists the implementation classes for the local JDBC object extractor for different data sources:

data source type description
websphere 4 and above data sources org . springframework.jdbc.support.nativejdbc.WebSphereNativeJdbcExtractor
weblogic 6.1+ and above version of the data source
jboss 3.2.4 and above version of the data source
C3P0 data source
DBCP data source oRg.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor
objectweb xapool Data source

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.