. VikingSteve , . OSGI Blueprint, XML, .
1) DBCP Apache Commons pom:
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
2) / :
<bean id="MydataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://DB-001:3306/Customer"/>
<property name="username" value="sys_ETL"/>
<property name="password" value="Blah"/>
<property name="initialSize" value="4"/>
<property name="maxActive" value="32"/>
<property name="maxIdle" value="16"/>
<property name="minIdle" value="8"/>
<property name="timeBetweenEvictionRunsMillis" value="1800"/>
<property name="minEvictableIdleTimeMillis" value="1800"/>
<property name="testOnBorrow" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="maxWait" value="1000"/>
<property name="removeAbandoned" value="true"/>
<property name="logAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="30000"/>
</bean>
bean, . bean Mydatasource. . , . , , .
3) POJO :
public class AccountInformationToDatabase {
private BasicDataSource dataSource;
public BasicDataSource getDataSource() {
return dataSource;
}
public void setDataSource(BasicDataSource dataSource) {
this.dataSource = dataSource;
}
@Handler
public void PersistRecord
(
@Body AccountRecordBindy msgBody
, @Headers Map hdr
, Exchange exch
) throws Exception
{
Connection conn = null;
PreparedStatement stmt=null;
try
{
conn= dataSource.getConnection();
stmt =conn.prepareStatement("SOME INSERT STATEMENT");
stmt.setString(1,msgBody.getAccountNumber().trim());
stmt.setString(2,msgBody.getRecordType().trim() );
stmt.setString(3,msgBody.getSequenceNumber().trim());
stmt.setString(4,msgBody.getTitle().trim());
stmt.setString(5,msgBody.getCustomerType().trim());
stmt.setString(6,msgBody.getName().trim());
stmt.setString(7,msgBody.getAccountAddress1().trim());
stmt.executeUpdate();
}
catch (Exception e)
{
throw new Exception(e.getMessage());
}
finally
{
try
{
if (stmt!=null)
{
stmt.close();
stmt= null;
}
if (conn!=null)
{
conn.close();
conn= null;
}
}
catch(SQLException e)
{
throw new Exception(e.getMessage());
}
}
}
}
POJO , datasource, org.apache.commons.dbcp.BasicDataSource. Mydatasource bean POJO, .
4) POJO bean :
<bean id="AccountPersist" class="AccountInformationToDatabase">
<property name="dataSource" ref="MydataSource"/>
</bean>
, ..