免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3239 | 回复: 6
打印 上一主题 下一主题

动态调用中提示未打开数据库 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-03-12 10:33 |只看该作者 |倒序浏览
我在AIX系统下主程序通过dlopen的方式动态调用子程序,主程序中打开了数据库,
如果将dlopen所在的函数编译成动态库,子程序能正常运行。
如果将dlopen所在函数编译成静态库,子程序中访问数据库报未打开数据库。
不知道是编译参数少了还是其他什么原因,请指教。
程序如下:

main.ec

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dlfcn.h>

EXEC SQL include sqlca;

#ifndef        SQLCODE
#define        SQLCODE        sqlca.sqlcode
#endif

int main()
{
EXEC SQL BEGIN DECLARE SECTION;
        char        *dbname;
        char        s_rq[9];
EXEC SQL  END  DECLARE SECTION;

        dbname = getenv("DBNAME");

        EXEC SQL DATABASE :dbname;
        if( SQLCODE ) {
                printf("[%s-%d] [%d]\n",__FILE__,__LINE__,SQLCODE);
                exit (-1);
        }

        EXEC SQL SELECT rq INTO :s_rq FROM table1;
        if( SQLCODE ) {
                printf("[%s-%d] [%d]\n",__FILE__,__LINE__,SQLCODE);
                exit (-1);
        }
        printf("In main rq:[%s]\n",s_rq);

        dllcall("libmytest.so","getrq");

        EXEC SQL CLOSE DATABASE;
        exit (0);
}

getrq.ec

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

EXEC SQL include sqlca;

int getrq()
{
EXEC SQL BEGIN DECLARE SECTION;
        char        s_rq[9];
EXEC SQL  END  DECLARE SECTION;

        bzero(s_rq,sizeof(s_rq));
        EXEC SQL SELECT rq INTO :s_rq FROM table1;
        if( SQLCODE ) {
                printf("[%s-%d] [%d]\n",__FILE__,__LINE__,SQLCODE);
                return -1;
        }
        printf("In getrq: RQ:[%s]\n",s_rq);
        return 0;
}


dllcall.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <dlfcn.h>

int dllcall( char *dllname, char *funname )
{
        void    *handle;
        int     (*func)();
        int                ret;

        if( (handle=dlopen(dllname,RTLD_LAZY)) == NULL ) {
                printf("打开动态库失败!\n");
                return (-1);
        }

        if( (func = (int (*)())dlsym(handle,funname)) == NULL ) {
                printf("打开函数失败!\n");
                return (-1);
        }

        ret = func();

        dlclose(handle);

        return ret;
}

makefile

all: libmytest.so libdll_a.a libdll.so a b

libdll.so:dlldrv.o
        cc -G -bexpall -bnoentry -o $@ dlldrv.o

libdll_a.a:dlldrv.o
        ar -urv $@ dlldrv.o

libmytest.so:getrq.o
        esql -G -bexpall -bnoentry -o $@ getrq.o

a:main.o
        esql -O -o $@ main.o -L. -ldll_a

b:main.o
        esql -brtl -O -o $@ main.o -L. -ldll

.SUFFIXES:.o .c .ec
.c.o:
        cc -g -c $<

.ec.o:
        esql -g -c $<
        rm -f $*.c

clean:
        rm -f a b *.so *.a *.o


编译后的执行程序
a是通过静态库调用的
执行结果:
In main rq:[20130107]
[getrq.ec-16] [-349]

finderr 349
-349   Database not selected yet.

b是通过动态库调用的
执行结果:
In main rq:[20130107]
In getrq: RQ:[20130107]


哪位大侠指教下。谢谢!

论坛徽章:
11
金牛座
日期:2015-03-19 16:56:22数据库技术版块每日发帖之星
日期:2016-08-02 06:20:00数据库技术版块每日发帖之星
日期:2016-04-24 06:20:00数据库技术版块每日发帖之星
日期:2016-04-13 06:20:00IT运维版块每日发帖之星
日期:2016-04-13 06:20:00数据库技术版块每日发帖之星
日期:2016-02-03 06:20:00数据库技术版块每日发帖之星
日期:2015-08-06 06:20:00季节之章:春
日期:2015-03-27 15:54:57羊年新春福章
日期:2015-03-27 15:54:37戌狗
日期:2015-03-19 16:56:41数据库技术版块每日发帖之星
日期:2016-08-18 06:20:00
2 [报告]
发表于 2013-03-12 15:36 |只看该作者
debug个,print一下连接数据库。。。。。

论坛徽章:
0
3 [报告]
发表于 2013-03-12 17:09 |只看该作者
回复 1# happer_xc


    线程中数据库的链接资源是共享的,进程就得重新链接了。

论坛徽章:
0
4 [报告]
发表于 2013-03-13 10:31 |只看该作者
调用静态库就是起新的进程了吗? 感觉不是这样的

论坛徽章:
0
5 [报告]
发表于 2013-03-13 16:24 |只看该作者
回复 4# happer_xc


    不是库的问题,是dlopen的原因吧?静态库在编译的时候已经收集到执行程序里了。你试试把dlopen直接写到main里,我估计也一样。

论坛徽章:
0
6 [报告]
发表于 2013-03-13 21:37 |只看该作者
回复 5# 3sane


    是的,编在main函数里也是一样。

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:15数据库技术版块每日发帖之星
日期:2015-11-26 06:20:00数据库技术版块每日发帖之星
日期:2016-02-25 06:20:00
7 [报告]
发表于 2013-03-15 10:21 |只看该作者
建议用connect/disconnent代替database/close database
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP