From 3b6af779fe3590c527d98cea320c996be1e5a119 Mon Sep 17 00:00:00 2001 From: lizhenzhen Date: Wed, 20 Mar 2024 16:43:35 +0800 Subject: [PATCH] 11 --- .../informix/entity/mybatis/Vccinfo.java | 6 +- .../beagle/informix/mapper/VccinfoMapper.java | 3 + .../impl/InformixJdbcProxyServiceImpl.java | 88 ++++++++++++------- .../impl/InformixMybatisServiceImpl.java | 20 ++--- src/main/resources/application.yaml | 34 ++++--- src/main/resources/init.sql | 8 +- src/main/resources/test.txt | 1 + .../service/impl/InformixServiceTest.java | 16 +++- 8 files changed, 106 insertions(+), 70 deletions(-) create mode 100644 src/main/resources/test.txt diff --git a/src/main/java/com/beagle/informix/entity/mybatis/Vccinfo.java b/src/main/java/com/beagle/informix/entity/mybatis/Vccinfo.java index 645a4eb..8c1c511 100644 --- a/src/main/java/com/beagle/informix/entity/mybatis/Vccinfo.java +++ b/src/main/java/com/beagle/informix/entity/mybatis/Vccinfo.java @@ -31,11 +31,13 @@ public class Vccinfo implements Serializable { private String updateKey; @TableField(value = "tclob") private String tClob; + @TableField(value = "tbytea") + private byte[] tBytea; /** * 如果是pg等数据库,字段声明为bytea,则不需要BlobTypeHandler转换,如果声明为blob,则需要转换 */ -// @TableField(value = "tblob", typeHandler = org.apache.ibatis.type.BlobTypeHandler.class) - @TableField(value = "tblob") + @TableField(value = "tblob", typeHandler = org.apache.ibatis.type.BlobTypeHandler.class) +// @TableField(value = "tblob") private byte[] tBlob; } diff --git a/src/main/java/com/beagle/informix/mapper/VccinfoMapper.java b/src/main/java/com/beagle/informix/mapper/VccinfoMapper.java index 2d560dd..021b42f 100644 --- a/src/main/java/com/beagle/informix/mapper/VccinfoMapper.java +++ b/src/main/java/com/beagle/informix/mapper/VccinfoMapper.java @@ -3,6 +3,7 @@ package com.beagle.informix.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.beagle.informix.entity.mybatis.Vccinfo; import org.apache.ibatis.annotations.*; +import org.springframework.stereotype.Repository; import java.util.List; @@ -19,4 +20,6 @@ public interface VccinfoMapper extends BaseMapper { * @param vccinfo */ void insertData(@Param("vccinfo") Vccinfo vccinfo); + + int selectTest(); } diff --git a/src/main/java/com/beagle/informix/service/impl/InformixJdbcProxyServiceImpl.java b/src/main/java/com/beagle/informix/service/impl/InformixJdbcProxyServiceImpl.java index 4ab92a9..ed3b55c 100644 --- a/src/main/java/com/beagle/informix/service/impl/InformixJdbcProxyServiceImpl.java +++ b/src/main/java/com/beagle/informix/service/impl/InformixJdbcProxyServiceImpl.java @@ -11,6 +11,10 @@ import java.io.*; import java.nio.charset.StandardCharsets; import java.sql.*; import java.util.Properties; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** * @author lzz @@ -28,6 +32,13 @@ public class InformixJdbcProxyServiceImpl implements InformixService { this.resourceLoader = resourceLoader; } + public static void main(String[] args) { + try { + new InformixJdbcProxyServiceImpl(null).doConnectJob(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } /** * informix.url=jdbc:informix-sqli://localhost:9088/sysadmin:INFORMIXSERVER=informix * informix.driverClassName=com.informix.jdbc.IfxDriver @@ -40,17 +51,38 @@ public class InformixJdbcProxyServiceImpl implements InformixService { */ @Override public void doConnectJob() throws Exception{ - // todo 使用jdbc连接proxy,对应的query(clob、blob字段)存在jdbc 类型转换mysql异常的问题 - // Unknown exception: Can not find JDBC type `2005` in column type + Driver driver = getDriver(); + Connection connection = getConnection(driver); + insert(connection); + closeConnection(connection); + deregisterDriver(driver); + } + private void testSameConnectionJob(Connection connection) throws Exception{ Driver driver = getDriver(); Connection conn = getConnection(driver); - insert(conn); -// update(conn); -// query(conn); -// delete(conn); + // 连接一直开启的话,发送的sql在proxy中都是使用一个同一个会话 + for (int i = 0; i < 100; i++) { + String querySQL = "SELECT 1"; + Statement stat = connection.createStatement(); + ResultSet resultSet = stat.executeQuery(querySQL); + } closeConnection(conn); deregisterDriver(driver); } + + private void testAloneConnectionJob() throws Exception{ + Driver driver = getDriver(); + for (int i = 0; i < 100; i++) { + // 这种情况下的话,每次新建一个连接,在proxy中都会对应新增一个连接 + Connection conn = getConnection(driver); + String querySQL = "SELECT 1"; + Statement stat = conn.createStatement(); + ResultSet resultSet = stat.executeQuery(querySQL); + closeConnection(conn); + } + deregisterDriver(driver); + } + private Driver getDriver() throws Exception{ // 定义informix的驱动信息 String driverUrl = "com.mysql.cj.jdbc.Driver"; @@ -66,7 +98,7 @@ public class InformixJdbcProxyServiceImpl implements InformixService { // 创建testdb数据库、my_test_create_table表 // 现在模拟插入、更新、删除、查询等sql // 插入 - String insertSql = "insert into cti_vccinfo_new (vccid ,vccname, effective, agentmax,ivrmax,updatekey,tclob,tblob) values (?, ? , ?, ?, ? , ?,?,? )"; + String insertSql = "insert into SYSDBA.cti_vccinfo_new (vccid ,vccname, effective, agentmax,ivrmax,updatekey,tclob,tbytea,tblob) values (?, ? , ?, ?, ? , ?,?,?,? )"; PreparedStatement stat = conn.prepareStatement(insertSql); stat.setObject(1, "123456"); stat.setObject(2, "vname"); @@ -79,11 +111,16 @@ public class InformixJdbcProxyServiceImpl implements InformixService { StringReader reader = new StringReader(clobContent); stat.setClob(7, reader, clobContent.length()); // 处理blob字段 - Resource resource = resourceLoader.getResource("classpath:EnableLoopback.exe"); + Resource resource = resourceLoader.getResource("classpath:test.txt"); File inputStream = resource.getFile(); FileInputStream fis = new FileInputStream(inputStream); stat.setBlob(8, fis); + resource = resourceLoader.getResource("classpath:test.txt"); + inputStream = resource.getFile(); + fis = new FileInputStream(inputStream); + stat.setBlob(9, fis); stat.executeUpdate(); + } private void update(Connection conn) throws Exception{ // 创建testdb数据库、my_test_create_table表 @@ -94,28 +131,13 @@ public class InformixJdbcProxyServiceImpl implements InformixService { stat.executeUpdate(); } private void query(Connection conn) throws Exception { - String querySQL = "select * from cti_vccinfo_new where vccid = '123456' "; - PreparedStatement stat = conn.prepareStatement(querySQL); - ResultSet resultSet = stat.executeQuery(); - if (resultSet != null) { - while (resultSet.next()) { - log.info("vccid =>" + resultSet.getString("vccid")); - Clob clob = resultSet.getClob("tclob"); - BufferedReader reader = new BufferedReader(clob.getCharacterStream()); - String clobStr = ""; - String finalClobStr = ""; - while ((clobStr = reader.readLine())!= null) { - finalClobStr += clobStr; - } - log.info("tclob =>" + finalClobStr); - Blob blob = resultSet.getBlob("tblob"); - BufferedInputStream bins= new BufferedInputStream(blob.getBinaryStream()); - byte [] bytes = bins.readAllBytes(); - String blobStr= new String(bytes, StandardCharsets.UTF_8); - log.info("tblob =>" + blobStr); - } - } + String querySQL = "SELECT id FROM sc_test"; + Statement stat = conn.createStatement(); + ResultSet resultSet = stat.executeQuery(querySQL); } + + + private void delete(Connection conn) throws Exception{ String deleteSql = "delete from cti_vccinfo_new where vccid = '123456' "; conn.createStatement().executeUpdate(deleteSql); @@ -125,9 +147,8 @@ public class InformixJdbcProxyServiceImpl implements InformixService { Properties properties = new Properties(); // 组装连接数据库信息 // jdbc:informix-sqli://:/:informixserver= - StringBuilder jdbcBuilder = new StringBuilder("jdbc:mysql://localhost:3307") - .append("/") - .append("testdb").append("?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&useServerPrepStmts=true"); + StringBuilder jdbcBuilder = new StringBuilder("jdbc:mysql://localhost:3307/test?useServerPrepStmts=true"); + //;.append("?useServerPrepStmts=true"); DriverManager.setLoginTimeout(10); properties.put("user", "root"); properties.put("password", "Apurelove9014"); @@ -136,4 +157,7 @@ public class InformixJdbcProxyServiceImpl implements InformixService { private void closeConnection(Connection connection) throws Exception { connection.close(); } + + + } diff --git a/src/main/java/com/beagle/informix/service/impl/InformixMybatisServiceImpl.java b/src/main/java/com/beagle/informix/service/impl/InformixMybatisServiceImpl.java index 085b535..026ea5f 100644 --- a/src/main/java/com/beagle/informix/service/impl/InformixMybatisServiceImpl.java +++ b/src/main/java/com/beagle/informix/service/impl/InformixMybatisServiceImpl.java @@ -2,22 +2,20 @@ package com.beagle.informix.service.impl; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.beagle.informix.entity.mybatis.Vccinfo; import com.beagle.informix.mapper.VccinfoMapper; import com.beagle.informix.service.InformixService; import lombok.extern.slf4j.Slf4j; -import org.apache.ibatis.annotations.Result; -import org.apache.ibatis.annotations.Results; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import javax.transaction.Transactional; import java.io.File; import java.io.FileInputStream; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** * @author lzz @@ -30,15 +28,9 @@ import java.util.List; public class InformixMybatisServiceImpl extends ServiceImpl implements InformixService { @Resource private ResourceLoader resourceLoader; - @Resource - private VccinfoMapper vccinfoMapper; @Override - @Transactional public void doConnectJob() throws Exception{ insertMethod(); -// updateMethod(); -// queryMethod(); -// delete(); } private void insertMethod() throws Exception{ Vccinfo vccinfo = new Vccinfo(); @@ -57,10 +49,16 @@ public class InformixMybatisServiceImpl extends ServiceImpl vccinfoList = vccinfoMapper.queryList(1); + List vccinfoList = this.baseMapper.queryList(1); log.info("queryAll:{}", JSONUtil.toJsonStr(vccinfoList)); } private void delete() { diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 2b2ede3..add463b 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -1,27 +1,25 @@ spring: datasource: - druid: -# url: jdbc:informix-sqli://localhost:9088/testdb:INFORMIXSERVER=informix - #&useServerPrepStmts=true # url: jdbc:mysql://localhost:3307/testdb?useUnicode=true&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai - url: jdbc:mysql://localhost:3307/testdb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai + # url: jdbc:mysql://localhost:3307/test.txt + url: jdbc:mysql://localhost:3307/test?useServerPrepStmts=true # url: jdbc:mysql://localhost:3307/testdb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&useServerPrepStmts=true username: root -# username: informix -# password: in4mix password: Apurelove9014 -# max-active: 20 -# max-wait: 60000 -# initial-size: 5 -# connect-timeout: 30 -# min-idle: 5 -# min-evictable-idle-time-millis: 300000 -# validation-query: select 1 -# test-on-borrow: false -# test-on-return: false -# test-while-idle: false -# time-between-eviction-runs-millis: 60000 -# driver-class-name: com.informix.jdbc.IfxDriver +# type: com.alibaba.druid.pool.DruidDataSource +# druid: +# max-active: 500 +# max-wait: 60000 +# initial-size: 500 +# connect-timeout: 30 +# min-idle: 20 +# min-evictable-idle-time-millis: 300000 +# validation-query: select 1 +# test.txt-on-borrow: false +# test.txt-on-return: false +# test.txt-while-idle: false +# time-between-eviction-runs-millis: 60000 + # driver-class-name: com.informix.jdbc.IfxDriver driver-class-name: com.mysql.jdbc.Driver # com.mysql.cj.jdbc.Driver jpa: properties: diff --git a/src/main/resources/init.sql b/src/main/resources/init.sql index b054275..d469169 100644 --- a/src/main/resources/init.sql +++ b/src/main/resources/init.sql @@ -1,15 +1,15 @@ -- serial 代表自增,本质上也是个int类型 create table cti_vccinfo_new ( - vccid serial not null, + vccid int not null primary key auto_increment, vccname VARCHAR(255), effective INTEGER default 0 not null, agentmax INTEGER default 0 not null, ivrmax INTEGER default 0 not null, updatekey VARCHAR(30), - tclob clob null, - tblob blob null, - primary key (vccid) constraint PK_CTI_VI_new + info longtext, + tclob longtext null, + tblob blob null ); -- 对接proxy,现在 dml 的 crud的sql都能正常识别并运行 diff --git a/src/main/resources/test.txt b/src/main/resources/test.txt new file mode 100644 index 0000000..a6fa76e --- /dev/null +++ b/src/main/resources/test.txt @@ -0,0 +1 @@ +this is test text \ No newline at end of file diff --git a/src/test/java/com/beagle/informix/service/impl/InformixServiceTest.java b/src/test/java/com/beagle/informix/service/impl/InformixServiceTest.java index 856ffcf..e742701 100644 --- a/src/test/java/com/beagle/informix/service/impl/InformixServiceTest.java +++ b/src/test/java/com/beagle/informix/service/impl/InformixServiceTest.java @@ -13,12 +13,12 @@ import javax.annotation.Resource; @SpringBootTest(classes = BaseApplication.class) class InformixServiceTest { -// @Resource(name = "informixJdbcService") -// private InformixService informixJdbcService; + @Resource(name = "informixJdbcService") + private InformixService informixJdbcService; @Resource(name = "informixJdbcProxyService") private InformixService informixJdbcProxyService; - @Resource(name = "informixHibernateService") +// @Resource(name = "informixHibernateService") private InformixService informixHibernateService; // @Resource(name = "informixIbatisService") // private InformixService informixIbatisService; @@ -57,4 +57,14 @@ class InformixServiceTest { e.printStackTrace(); } } + + @Test + void testJdbcInfo() { + try { + informixJdbcService.doConnectJob(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } -- 2.26.0