Connection Shareable是对XA资源的一个优化。
原因: XA资源的提交代价比较大,如果同一个应用(要求在同一个线程中)连续得到的两个XA链接都是同一个EIS时,那么就可以优化成一个连接,变为了LocalTransaction,提高了性能。
========
测试代码
========
com.sun.appserv.jdbc.DataSource res_unshare = (com.sun.appserv.jdbc.DataSource)ctx.lookup("java:comp/env/DataSourceNoShare");
com.sun.appserv.jdbc.DataSource res_share = (com.sun.appserv.jdbc.DataSource)ctx.lookup("java:comp/env/DataSourceShare");
for(int i=0; i<num; i++) {
conns[i] = shareable ? res_share.getConnection(res_share.getConnection())
: res_unshare.getConnection(res_unshare.getConnection());
rt[i] = conns[i].toString();
}
========
ejb-jar.xml
========
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0">
<enterprise-beans>
<session>
<ejb-name>jcatest</ejb-name>
<local>com.jca.test.JCAPool_InitialLocal</local>
<resource-ref>
<res-ref-name>DataSourceNoShare</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Unshareable</res-sharing-scope>
<mapped-name>jdbc/__default</mapped-name>
</resource-ref>
<resource-ref>
<res-ref-name>DataSourceShare</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
<mapped-name>jdbc/__default</mapped-name>
</resource-ref>
</session>
</enterprise-beans>
</ejb-jar>