JBoss is a very powerful J2EE application server, but this powerful function also brings complexity. This article author Marcus Zarra analyzes how to configure multiple databases and data sources in JBOSS. Configuring the default installation process of the configuration data source JBoss has three configuration options: all (all), defaults, and minimum. If you have an incident with any parameters when you start the JBOSS server, it naturally runs the default configuration. If you need to run other configurations, you must run the startup script with the -c parameter, as shown below: $ {jboss.root} /bin/run.sh -c minimal Start JBoss with this way, indicating that it uses the smallest server configuration Instead of the default configuration. But when trying to change the configuration of JBoss itself, this technology is very useful. You can modify your operation in different configurations, if necessary, roll back to the default configuration. However, you should copy the default configuration before conflicting the data source so that you can safely operate the copy (in this article, I call your own copy as an Example, so there are four configurations in the server directory. Information: All, default, minimal, and examples. The first step in changing the default data source in JBoss is to configure a new data source. All database configurations in JBoss are defined in the deploy directory in the server directory you work (Server / Example / Deploy in the example). The default database used by JBoss is a pure Java database Hypersonic. The configuration information of this database is located in the HSQLDB-DS.xml file. This XML configuration file describes how to connect to the Hypersonic database. After deleting all comments in this file, we get the configuration information as shown below:
XML Version = "1.0" encoding = "UTF-8"?> defaultds jndi-name> JDBC: HSQLDB: $ { JBoss.server.Data.dir} $ {/ }hip} $ {/} localdb connection-url> org.hsqldb.jdbdriver driver-class> sa user- Name> password> 5 min-pool-size> 20 max-pool-size> 0 < / idle-timeout-minutes> HSQLDBREALM security-domain> jboss: service = hypersonic, Database = locAldb depends> local-tx-datasource> < mbean code = "org.jboss.jdbc.HypersonicDatabase" name = "jboss: service = Hypersonic, database = localDB"> localDB attribute> true Attribute> mbean> datasources> Most of these parameters are similar to the parameters of JDBC. The first part under root (root is . This is where you define the database and how to connect the database: drivers, URLs, usernames, and passwords. You also define how many connections should be in the connection pool. The second step contains two interesting parameters: · Allows you to configure the username and password of the database in the login-config.xml file instead of data source configuration information. · Specifies that the service is only launched after the service it is dependent. This parameter allows you to write a database that the database should run correctly. Tag also indicates that JBOSS will close some data items as needed.
This tag in the example indicates that the MBean must start before the database is started, and after the database is turned off. After defining the data source, you can use it. Any other service or configuration that wants to use this data source must use the JNDI name to reference it. In the example, the JNDI name is DefaultDS. Add another data source To add another data source to JBOSS, you need to create a new profile. I want to establish a data source, which is connected to my native MySQL database and specifically uses an Example database. In order to achieve this goal, I built a similar configuration file with the front:
XML Version = "1.0" encoding = "UTF-8"?> Exampleds jndi-name> jdbc: mysql: // LocalHost / Example connection-url> Org.gjt.mm.mysql.driver driver-class> exampleUser user-name> eXamplePassword password> < MIN-POOL-SIZE> 5 min-pool-size> 20 max-pool-size> 0 idle-timeout-minutes> local-tx-datasource> datasources> I have established a data source called ExampleDS, which connects to a MySQL database using the username ExampleUser and password EXAMPLEPASSWORD via JDBC. I also specify at least 5 connections at least in the connection pool, up to 20 connections, and these connections cannot be idle. Then I store this file as an example-ds.xml in the deploy directory (this file is not important, but the name is consistent). Since my new data source doesn't need, I don't rely on any other module. There is no tag in the configuration information. At the same time, because I specified the username and password used to use the database in this file, I don't need to refer to the security domain (Security Domain). Using new data sources now I want to move some of the services in JBoss to my new data source. It is very simple to: Although HyperSonic is a good database, it should not be used for product environment and / or high-traffic systems; MySQL and several other databases can better play this role. One of the most common parts of the Java Message Services JBoss Server is the message service. This part of the server is very large and should use my new data source. The following two files in the deploy / jms directory define how messaging services use data sources: · hsqldb-jdbc-state-service.xml · hsqldb-jdbc2-service.xml first, I modified one of the files to highlight the database Type: Turn HSQLDB-JDBC-State-Service.xml to mysql-jdbc-state-service.xml. I only have a little modification for mySQL-JDBC-State-Service.xml files: Tags should reference the new ExampleDs data source and not the default defaultds.