`
wj.king
  • 浏览: 68933 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

jboss,jbosscache,spring,mssql2000配置备忘

阅读更多
由于项目需要,要使用缓存作为验证系统,就产生了和数据库事务同步的需要,查来查去就觉得jbosscache可以,但是这个东西的中文资料太少,英文水平有差,看的头大,jta也是第一次运用,跌跌撞撞的现在有点眉目,先纪录下保存,还得继续配置。
一、jboss基本配置:
虚拟主机配置:
1、\server\default\deploy\jboss-web.deployer\server.xml设置host:
        <Host name="projectA"
           autoDeploy="false" deployOnStartup="false" deployXML="false"
           configClass="org.jboss.web.tomcat.security.config.JBossContextConfig">
        </Host>

2、设置\server\default\conf\jboss-service.xml文件的:
      <attribute name="URLs">
         deploy/,file:/E:/workspace/projectA
      </attribute>

    其中file:/E:/workspace/projectA为我的工程目录,里面有web.war文件夹作为jboss部署时候的选择文件夹(为什么非得要加这个后缀?而且如果这个文件夹或子文件夹有*.rar文件会报部署错误)

3、设置jboss-web.xml
    新增jboss-web.xml文件到projectA/web.war/WEB-INF下,内容如下
<?xml version="1.0"?>
<!DOCTYPE jboss-web>
<jboss-web>
	<context-root>/</context-root>
	<virtual-host>projectA</virtual-host>
</jboss-web>


4、设置mssql-xa-ds.xml:
    新增mssql-xa-ds.xml文件到/E:/workspace/projectA下(和web.war文件同一目录下,为什么也还不清楚,需要了解),内容如下(用的是jtds驱动):
<?xml version="1.0" encoding="UTF-8"?>

<datasources>
	<xa-datasource>
		<jndi-name>MSSQLXADS</jndi-name>
		<track-connection-by-tx />
		<use-java-context>false</use-java-context>
		<isSameRM-override-value>false</isSameRM-override-value>
		<xa-datasource-class>
			net.sourceforge.jtds.jdbcx.JtdsDataSource
		</xa-datasource-class>
		<xa-datasource-property name="ServerName">
			localhost
		</xa-datasource-property>
		<xa-datasource-property name="DatabaseName">
			dbname
		</xa-datasource-property>
		<xa-datasource-property name="Tds">8.0</xa-datasource-property>
		<user-name>sa</user-name>
		<password>sa</password>
		<!--
			You are recommended to use prepareSql=2 with true distributed transactions.
			If a temp stored procedure is created and then the container rolls back the 
			transaction, the driver's procedure cache will be out of line with the server.
		-->
		<xa-datasource-property name="PrepareSql">
			2
		</xa-datasource-property>
		<!--
			Set this property to true to cause the driver to emulate distributed transactions.
			False option only works for SQL Server 2000 with MSDTC and JtdsXA.DLL installed.
		-->
		<xa-datasource-property name="XaEmulation">
			false
		</xa-datasource-property>

		<min-pool-size>10</min-pool-size>
		<max-pool-size>60</max-pool-size>
		<idle-timeout-minutes>15</idle-timeout-minutes>
		<blocking-timeout-millis>5000</blocking-timeout-millis>
		<new-connection-sql>select 1</new-connection-sql>
		<check-valid-connection-sql>
			select 1
		</check-valid-connection-sql>
		<set-tx-query-timeout />
		<metadata>
			<type-mapping>MS SQLSERVER2000</type-mapping>
		</metadata>
	</xa-datasource>

</datasources>

    注意:需要把jtds包中\x86\XA下的JtdsXA.dll加入mssql(方法:拷贝到sqlserver的BINN里面,在查询分析器执行\x86\XA下的instjtds.sql)
    参考:http://wiki.jboss.org/wiki/Wiki.jsp?page=ConfigDataSources

二、spring配置使用jta
主要就是配置datasource,根据spring文档,直接copy设置即可,不过有个问题搞了满久,就是jee中的jndi的value设置只要和mssql-xa-ds.xml中的jndi-name相同即可,中间搞了满久加java:之类的都报错,后来恼火直接用就好了(JNDI没用过,要学的真tmd多)
大致配置如下:
	<jee:jndi-lookup id="dataSource" jndi-name="MSSQLXADS" />
	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />

PS:赞一个spring,呵呵,从jdbc的换到jta还真方便啊

三、jbosscache设置
jbosscache一开始就被网上很多介绍说一堆一堆的很神秘的配置吓倒,其实仔细看看英文文档,对照需要的功能,应该没那么恐怖的,不过现在还确实什么都不明白,事务啊,锁啊都是很晕,留着慢慢边应用边学习了,
由于并不需要集群,我只想要事务和锁,所以就用了local model的配置,大概配置如下
<?xml version="1.0" encoding="UTF-8"?>
<server>
	<mbean code="org.jboss.cache.jmx.CacheJmxWrapper"
		name="jboss.cache:service=Cache">

		<!-- Configure the TransactionManager -->
		<attribute name="TransactionManagerLookupClass">
			org.jboss.cache.transaction.GenericTransactionManagerLookup
		</attribute>

		<!-- Node locking level : SERIALIZABLE			
			REPEATABLE_READ (default)			
			READ_COMMITTED			
			READ_UNCOMMITTED			
			NONE             -->
		<attribute name="IsolationLevel">READ_COMMITTED</attribute>
		
		<!-- Lock parent before doing node additions/removes -->
		<attribute name="LockParentForChildInsertRemove">
			true
		</attribute>

		<!-- Valid modes are LOCAL (default)			
			REPL_ASYNC			
			REPL_SYNC			
			INVALIDATION_ASYNC			
			INVALIDATION_SYNC   -->
		<attribute name="CacheMode">LOCAL</attribute>

		<!-- Max number of milliseconds to wait for a lock acquisition -->
		<attribute name="LockAcquisitionTimeout">15000</attribute>

	</mbean>
</server>


简单使用方式:
public class CacheManagerJbossCacheImpl {
    private static final Log logger = LogFactory
            .getLog(CacheManagerJbossCacheImpl.class);
    private Cache<String, Object> cache;

    public void initCache() {
        logger.info("initCache CacheManagerJbossCacheImpl");
        CacheFactory factory = new DefaultCacheFactory();
        this.cache = factory.createCache(
                "com/projecta/cache/jbosscache-config.xml", true);
        cache.addCacheListener(new JbossCacheListener());
    }

    public void insertDepartmentToCache(Department d) {
        // Fqn<String> departmentFqn = Fqn.fromString("/department/" +
        // d.getBh());
        this.cache.put("/department/" + d.getBh(), d.getBh(), d);
        logger.info("put cache " + d.getBh());
    }

    public Department getDepartmentFromCache(String bh) {
        return (Department) this.cache.get("/department/" + bh, bh);
    }

    public void destoryCache() {
        this.cache.stop();
        this.cache.destroy();
    }

}

该类使用spring的bean设置init-method来启动initCache


暂时要解决的问题:
1、偶尔出现javax.transaction.xa.XAException: XAER_RMFAIL: The resource manager is unavailable错误,网上有资料,英文的,看的意思好像是说要配置连接池的大小,加上后好像没点出来,还得继续处理
2、jbosscache的文档还得读,英文的,痛苦,有没好心人翻译下额- -
3、jboss启动瘦身处理,以前都用的tomcat,第一次用jboss,好晕,不知如何下手
4、jta原理了解,jta是不是由容器提供一个事务管理器,然后每个参与事务的连接都由它管理?
5、jndi,,,,,,话说做过好久j2ee了,其实用到这些的机会少之又少,学吧
6、jbosscache是如何参与事务的???虽然测试已经通过,,,
还有?以后加,,,
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics