- 浏览: 544952 次
- 性别:
- 来自: 西安
文章分类
- 全部博客 (251)
- UML/RUP/软件工程 (0)
- DDD:领域驱动设计 (1)
- IaaS/Paas/SaaS (1)
- Hadoop/YARN (10)
- PBDP项目 (2)
- OSGi-基础 (7)
- OSGi-Aries (2)
- OSGi-SpringDM (32)
- Oracle/MySQL/SS (8)
- Felix/Equinox/Bnd (14)
- Virgo Server/Gemini (7)
- Weblogic/JBoss/Tomcat (10)
- ActiveMQ (14)
- Camel (1)
- Spring Roo/Eclipse (7)
- Java/JSP/JSF (10)
- Maven (19)
- ESB-Mule (1)
- ESB-ServiceMix (18)
- SOA/SCA/SDO (12)
- WebService/RESTful (17)
- JS/jQuery/ExtJS (4)
- Spring/JPA/MVC (15)
- SpringSecurity (5)
- BPM/jBPM (3)
- Hudson/SVN/CI (0)
- LinuxOS/虚拟化 (25)
- Office/OpenOffice (1)
- 项目管理 (5)
- POI/jFreeChart (1)
最新评论
-
panamera:
请问JMS Transport 发布的webservice 是 ...
CXF 提供的Service Transport-JMS Transpor -
jianyi296:
where is attachment.
CXF WebService Dynamic Client -
hj01kkk:
您好,我用jdbc-user-service为什么每次验证时都 ...
SpringSecurity笔记3-Authenticating Users:Authenticaton Strategy -
wufenglin1231:
你好,我在实现Mapping exceptions to re ...
RESTful 异常处理 -
xchd:
[echo] Project: common
[echo ...
Hive安装配置学习笔记
The public API for interacting with databases is provided directly in Java SE within the java.sql
package. This package provides the key abstractions corresponding to connections, statements,
result sets, and metadata。
1. JDBC连接数据库
(1)Common entities provided by JDBC:
■ Connection:Corresponds to the connection to the database, allowing the execution of SQL requests
and being responsible for handling local transactions.
■ Statement:Corresponds to the core abstraction for executing all kinds of SQL requests.
■ PreparedStatement
:Corresponds to an advanced statement that supports precompiled SQL queries
and parameter specification
■ CallableStatement:CallableStatement Corresponds to a kind of statement enabling you to execute
stored procedures with JDBC.
■ ResultSet:Allows for the parsing of data received on the execution of a SQL query.
■ DatabaseMetaData:Provides access to metadata of the target database.
■ ResultSetMetaData:Provides access to metadata about the types and properties of the columns
contained in a result set
(2)Using JDBC to execute a SQL select request:
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs= null;
try {
connection = getConnection();
String sql = "select id, last_name, first_name" + " from contact" + " where last_name like ?";
statement = connection.prepareStatement(sql);
statement.setString(1, "P%");
rs= statement.executeQuery();
while (rs.next()) {
String contactFirstName= rs.getString("first_name");
String contactLastName= rs.getString("first_name");
}
} catch(Exception ex) {
(...)
} finally {
try {
if (resultSet!=null) {
resultSet.close();
}
} catch(SQLException ex { (...) }
try {
if (statement!=null) {
statement.close();
}
} catch(SQLException ex { (...) }
try {
if (connection!=null) {
connection.close();
}
} catch(SQLException ex { (...) }
}
An extension to JDBC is also available to provide additional support for server-side usage through
the javax.sql package. The more well-known interface of this extension is the DataSource interface,
which corresponds to a connection factory and is commonly used by pool implementations. The
Spring Framework also provides implementations of the DataSource interface within its JDBC
support.
2. JDBC issues when used within OSGi
JDBC的底层实现是驱动程序:
public interface Driver {
boolean acceptsURL(String url);
Connection connect(String url, Properties info);
int getMajorVersion();
int getMinorVersion();
DriverPropertyInfo[] getPropertyInfo(String url, Properties info);
boolean jdbcCompliant();
}
而常见的Java应用程序使用JDBC的主要类是:DriverManager:
Class.forName("org.h2.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:h2:tcp://localhost/springdm-directory","sa", "");
The driver class name is loaded using the static forName method of the Class class. The driver can
then be used to create a JDBC connection using the DriverManager class’s static getConnection
method.
The problem is that the DriverManager class uses classloaders in a specific way that can have
strange side effects within an OSGi container. The class internally checks whether the caller class is
able to instantiate the driver class, and if the caller doesn’t have the required package imports in
its manifest configuration, unexpected ClassNotFoundException exceptions can occur. To avoid this
check, it’s better to not use the DriverManager class within an OSGi container.
The alternative is to use the driver interface directly to obtain a JDBC connection, as shown in the
following snippet. Note that Spring’s ClassUtils and BeanUtils classes can be used to safely
instantiate a JDBC driver from within an OSGi container.
Class driverClass = ClassUtils.resolveClassName(driverClassName,
ClassUtils.getDefaultClassLoader());
Driver driver = (Driver) BeanUtils.instantiateClass(driverClass);
Properties properties = new Properties();
properties.put("user", userName);
properties.put("password", password);
Connection connection = driver.connect(jdbcUrl, properties);
Moreover, when using a pool, you should ensure that it doesn’t use the DriverManager
class. The C3P0 library partially uses the DriverManager class to get a driver and then directly
works with the returned class. In this way, C3P0 uses classloaders correctly from the perspective
of an OSGi container.
3.Configuring JDBC data sources
JDBC provides an abstraction named DataSource, which defines a contract for providing connections.
A DataSource supports provider implementations according to your required resource management
strategy (direct use of driver, pools, and so on).
Different data source implementation strategies for creating JDBC connections:
■ Pool data source
■ DriverManager-based data source
■ Driver-based data source
(1)CONFIGURING A POOL BUNDLE
Database connection pool classes need to see driver packages because the classes contained in
these packages manage the connection resources. By default, the components corresponding to
the pools don’t have the correct OSGi package configuration in their JAR manifest entries. Several
strategies exist to address this issue:
■ During the packaging or OSGi-ification of the pool component, you can add optional import entries
for each package for the possible drivers that you might use. 方法1
■ You can implement a fragment bundle that will add the package containing the driver class that’s
used to the pool component’s manifest configuration.方法2
方法1配置:
Specifying driver packages within the Bnd configuration:
version=1.2.2
Bundle-SymbolicName: org.apache.commons.dbcp
Bundle-Version: ${version}
(...)
Import-Package:org.apache.commons.pool*;version=1.3.0,
org.apache.commons.dbcp*;version=${version},*;resolution:=optional,
org.hsqldb;version="1.8.0.9";resolution:=optional,
org.h2;version="0.0.0";resolution:=optional,
org.postgresql;version="0.0.0";resolution:=optional,
(...)
方法2配置:
The following snippet shows the manifest configuration of a fragment bundle that adds the H2
package to the C3P0 component:
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: C3P0 Configuration Fragment
Bundle-SymbolicName:com.springsource.com.mchange.v2.c3p0.config
Bundle-Version: 1.0.0
Bundle-Vendor: Manning
Fragment-Host: com.springsource.com.mchange.v2.c3p0;bundle-version="0.9.1"
Import-Package: org.h2;version="0.0.0"
注意:The SpringSource repository provides a modified version of DBCP that also looks for drivers in
the context classloader. When using this version, you don’t need to add such a fragment, because
the driver will be present in this classloader during the pool’s initialization.
(2)CONFIGURING A DATA SOURCE WITH SPRING DM
Best practice consists of defining a dedicated bundle to configure the data source. Within this
bundle, Spring DM facilities can be used to configure a DataSource instance and register it as an
OSGi service. This service can then be referenced by data access components to interact with
the database.
Configuring a C3P0 pool with Spring DM:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/jdbc.properties"/>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<osgi:service ref="dataSource" interface="javax.sql.DataSource"/> //导出数据源最为一个OSGi服务
(3)Provisioning the OSGi container for JDBC
Common bundles for Spring JDBC support from SpringSource EBR:
org.springframework.transaction:3.0.5
org.springframework.jdbc:3.0.5
(4)Using JDBC within OSGi with Spring DM
■ REFERENCING JDBC DATA SOURCES IN SPRING DM:
<osgi:reference id="dataSource" interface="javax.sql.DataSource"/>
<osgi:reference id="dataSource" interface="javax.sql.DataSource" filter="(source='database1')"/>
<bean id="contactsDao"
class="com.manning.sdmia.dataaccess.domain.dao.jdbc.ContactsDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
■ USING THE JDBCTEMPLATE CLASS:
DAO structure using the Spring JDBC support:
public class ContactsDaoImpl extends JdbcDaoSupport
implements ContactsDao {
public List<Contact> getContacts() {
String sql = (...)
return getJdbcTemplate
().query(sql, new RowMapper() { (...) });
}
(...)
}
需在MAINFEST.MF文件中Import-Package:
Import-Package: (...)
org.springframework.beans.factory;version="3.0.0.M1",
org.springframework.core;version="3.0.0.M1",
org.springframework.dao;version="3.0.0.M1",
org.springframework.jdbc.core;version="3.0.0.M1",
org.springframework.jdbc.core.support;version="3.0.0.M1"
Bootstrap packages(引导包):
The javax.sql package is provided by the OSGi container as a bootstrap package , which means that
you don’t have to specify it explicitly in a bundle. You can specify which of the packages already
present in Java you want by setting your OSGi container’s configuration.
This configuration is specific to the container. For example, the Equinox container requires the use of
the org.osgi.framework.bootdelegation configuration property with a value such as
sun.*,com.sun.*,java.*,javax.*,org.w3c.*.
发表评论
-
SpringDM笔记31-Testing with OSGi and SpringDM
2011-11-22 10:27 12441. 创建一个SpringDM测试类 SpringD ... -
SpringDM笔记30-OSGi中使用SSL/STL
2011-11-21 11:55 1317SSL:Secure Sockets Layer ... -
SpringDM笔记29-Require-Bundle与Import-Package的区别
2011-11-21 10:31 2238具体可参考:http://www.osgi.org/bl ... -
SpringDM笔记28-OSGi Bundle Activities with Spring-DM
2011-11-17 10:19 1158OSGi框架中也支持搞层次的模块交互:bundles.例如 ... -
SpringDM笔记28-Spring And OSGi:Layers of Integration
2011-11-15 11:00 11721. Application Design:Service和B ... -
SpringDM笔记27-Extending The Stand Extender and Configure
2011-09-02 09:38 8691. -
SpringDM笔记25-Using AJAX frameworks with Spring DM:GWT
2011-09-01 08:53 12001. Using Spring DM with AJAX fr ... -
SpringDM笔记24-Using action-based web frameworks with Spring DM:SpringMVC
2011-08-30 09:33 1478■ Action-based web frameworks ... -
SpringDM笔记23-Using the open EntityManager in view pattern实现延迟加载
2011-08-30 09:27 15661. The open EntityManager in vi ... -
SpringDM笔记22-Transactions Support With SpringDM
2011-08-29 21:24 12101. Spring’s transactional suppo ... -
SpringDM笔记21-Using ORM within OSGi with Spring DM
2011-08-25 10:31 2095Version 1.Object/relational ma ... -
SpringDM笔记19-SpringDM如何处理OSGi应用的动态行为
2011-08-24 08:51 1142ServiceTracker 1. Dealing ... -
SpringDM笔记18-Designing OSGi Enterprise Applications
2011-08-22 11:08 11211. Organizing OSGi components ... -
SpringDM笔记17-Handling Collections of OSGi Services
2011-08-20 09:12 15411.Configuring collections:the l ... -
SpringDM笔记16-处理OSGi服务的动态性:事件
2011-08-19 09:51 17791. Service registration and unr ... -
SpringDM笔记15-通过声明特定的属性注册和引用服务
2011-08-18 11:01 14071. Configuration for registerin ... -
SpringDM笔记14-The thread context classloader 及在OSGi中的运用
2011-08-18 10:40 22471. Using the thread context cla ... -
SpringDM笔记13-OSGi服务注册与引用
2011-08-18 09:28 34491. Combining OSGi services and ... -
SpringDM笔记12-Spring DM’s web Extender运行机制
2011-08-17 11:04 2087SpringDM把一个WAR作为一个Bundle, 其实 ... -
SpringDM笔记11-Hard dependencies on the OSGi environment
2011-08-17 10:50 1413Any bean in a Spring DM app ...
相关推荐
NULL 博文链接:https://springsfeng.iteye.com/blog/1271565
spring-osgi-1.2.1-with-dependencies.zip spring-osgi-1.2.1-with-dependencies.zip spring-osgi-1.2.1-with-dependencies.zip
spring-osgi-1.2.0-rc1-with-dependencies.zip
spring-osgi-1.2.1-with-dependencies完整包
NULL 博文链接:https://springsfeng.iteye.com/blog/1259203
spring-osgi-1.2.0-with-dependencies.zip spring-osgi-1.2.0-with-dependencies.zip spring-osgi-1.2.0-with-dependencies.zip
NULL 博文链接:https://springsfeng.iteye.com/blog/1152012
spring-dm整合osgi所需所有jar包
Spring-DM指的是 Spring ...Spring-DM 的主要目的是能够方便地将 Spring 框架和OSGi框架结合在一起,使得使用Spring的应用程序可以方便简单地部署在OSGi环境中,利用OSGi框架提供的服务,将应用变得 更加模块化。
spring-osgi-core-1.2.1.jar spring-osgi-core-1.2.1-sources.jar spring-osgi-extender-1.2.1.jar spring-osgi-extender-1.2.1-sources.jar spring-osgi-io-1.2.1.jar spring-osgi-io-1.2.1-sources.jar spring-...
带有嵌入式OSGI的Spring Boot 这是一个嵌入了Felix OSGI框架的示例Spring Boot项目。 其他项目是API(接口和模型类)及其实现。 Spring Boot应用程序将这些程序包作为OSGI框架的额外程序包公开(以便能够使用公开的...
spring1.2的包文件 spring-osgi-1.2.0.zip
spring dm osgi 中的服务提供和模块化编程
学习SpringDM+OSGI的一些总结,自我学习,上传文档防止丢失。
httpcomponents-client-4.2-alpha1-osgi-bin.zip
httpcomponents-client-4.2-alpha1-osgi-bin.tar.gz
利用R-OSGi实现分布式OSGi应用 本文通过介绍传统 OSGi 应用程序及 R-OSGi 的实现方式入手,阐述了 R-OSGi 对于 OSGi 规范的实现方式。然后通过一个简单的功能实现由浅入深地讲述传统 OSGi 和 R-OSGi 上的两种不同...
Spring-DM指的是 Spring ...Spring-DM 的主要目的是能够方便地将 Spring 框架和OSGi框架结合在一起,使得使用Spring的应用程序可以方便简单地部署在OSGi环境中,利用OSGi框架提供的服务,将应用变得 更加模块化。
osig与spring:springDM开发环境配置,eclipse配置osgi开发环境,及命令
osgi springdm.pdf