kingquake21 发表于 2013-1-14 23:00:10

学习配置Sun JAVA System Application Server 中的数据源

使用netbean 6.0 ,JDK1.6 , J2EE 5 SDK ,Derby数据库
1.首先配置Connection Pool
开始一直在J2EE 5 SDK的控制台中配置,但是在配置Connection Pool时,利用默认的参数,并将DatabaseName,User,Password,driverClass,serverName分别配置好后,却无法Ping通。找了很长时间都没找到原因,后来发现利用netbean就能配置Connection Pool,方法:File->New File->->next-> …… 。利用这种方法配置好后,在J2EE 5 SDK的控制台中可以发现增加的这个Connection Pool,并且能够ping通。
比较了一下上面两种做法,发现在控制台中配置的Additional Properties与在netbean中配置的不同,增加了一个Url:jdbc:derby://localhost:1527/LoginInfo属性,关键是少了很多属性【默认的属性还有:SecurityMechanism=4,RetrieveMessageText=true,TraceFileAppend=false,TraceLevel=-1,LoginTimeout=0】,将SecurityMechanism这个属性delete掉后,或者将值改为3和8都能ping通。
注:SecurityMechanism,the valid values are 4(USRIDONL), 3(USRIDPWD), 9(EUSRIDPWD) and8(USRSSBPWD).2.然后配置JDBC Resource最初设置的JNDI Name:jdbc/login,选择好上面配置的Connection Pool。在sun-web.xml中设置如下: <resource-ref>
<res-ref-name>ds</res-ref-name>
<jndi-name>jdbc/login</jndi-name>
</resource-ref>程序中的使用方法: @Resource(name = "ds")
private DataSource ds;
con = ds.getConnection(); // 即应该可以使用,但是报错:【Caused by: com.sun.enterprise.InjectionException: Exception attempting to inject Res-Ref-Env-Property: ds@javax.sql.DataSource@ resolved as: jndi: jdbc/login@res principal: null@mail: null

改正方法:将JNDI Name改成login,在sun-web.xml中设置如下: <resource-ref>
<res-ref-name>ds</res-ref-name>
<jndi-name>login</jndi-name>
</resource-ref>成功!问题:
在程序中如何不用@Resource(name = "ds")这种方式,为什么使用
            InitialContext icontext = new InitialContext();
            Context   context   =   (Context) icontext.lookup("java:com/env");   
            DataSource ds = (DataSource) context.lookup("login");
却无法取到数据源?
页: [1]
查看完整版本: 学习配置Sun JAVA System Application Server 中的数据源