« Back to Using Liferay

Managing Instances

Table of Contents [-]

Introduction #

This article describes how to manage instances as asked several times in the forum and Jira. It gives you the ability to delete unwanted instance data.

Code #

To manage instances, and delete unwanted instance data, use the following code.

Note: Always backup first.

Note: When restoring this data in an existing database that problems could arise in tables like Counter. One could set these counters higher manually to much much higher values. It would be nice if these counters where updated with System.currentTimeMillis() so they would be most likely unique or Counter should get an companyId field.

public class LiferayInstanceRemovalMachine {

	public static void main(String[] args) {
		System.out.println("START");
		try {

			String originating_user = "root";
			String originating_password = "root";
			String originating_driverURL = "jdbc:mysql://localhost/lportal?useUnicode=true&" 
				+ "amp;characterEncoding=UTF-8&useFastDateParsing=false";
			String originating_Driver = "com.mysql.jdbc.Driver";
			Class.forName(originating_Driver);
			long companyId = 12345;
			
			Connection connection = java.sql.DriverManager.getConnection(originating_driverURL,
					originating_user,	originating_password);

			// ** LOAD SCHEMAS **//
			DatabaseMetaData metaData = connection.getMetaData();
			// Get driver information
			System.out.println("############ Driver Information ###########");
			System.out.println(metaData.getDriverName());
			System.out.println(metaData.getDriverVersion());

			HashSet<String> all_tables = new LinkedHashSet<String>();

			// Get table information
			System.out.println("########### Tables ############");
			ResultSet tables = metaData.getTables("", "", "", null);
			while (tables.next()) {
				all_tables.add(tables.getString(3));
				System.out.println(tables.getString(3));
			}

			
			for (String table : all_tables) {
				ResultSet rs = metaData.getColumns("", "", table, null);

				for (int cols = 1; cols <= rs.getMetaData().getColumnCount(); cols++) {
					System.out.print(rs.getMetaData().getColumnName(cols)+"\t\t");
				}
				while (rs.next()) {
					for (int cols = 1; cols <= rs.getMetaData().getColumnCount(); cols++) {
						String col = rs.getString(cols);
						System.out.print(col+"\t\t");
						if(col.equalsIgnoreCase("companyid")){
							// delete all from companyId
							//companyId
							String sql = "DELETE FROM "+table+" where companyid="+companyId;
							System.out.println(sql);
							connection.createStatement().execute(sql);
							break;
						}
					}
					System.out.println();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("END");
	}
}}
0 Attachments
8101 Views
Average (0 Votes)
Comments

Showing 4 Comments

Andrey Urvancev
6/20/09 11:06 AM

This erase data from database, but how remove instance from Document Library repository?

Milan Jaroš
7/23/09 10:46 AM

Hi. We found the easier way is to go to the portal instance and delete all the data via portlets. You can check the data in the database then (i. e. use query by this way: SELECT * FROM igimage WHERE companyid = XXXXX ).
Until there is no advanced managing of instances (remove, duplicate, backup... etc.) we decided to have every instance in separate database and Liferay...

Tom Thomas
10/2/09 12:51 PM

I am having trouble getting a Liferay (5.2.2)/JBoss bundle to display a new instance using the same database (SQLServer 2005). I have gone through serveral cycles of using the LiferayInstanceRemovalMachine program (thanks, btw) to remove the new instance and trying to add it again.
I confirmed that the instance is defined in DNS. Any ideas?

Fabrice Clari
10/29/10 12:56 AM

HI there,

Is that still the recommended solution?

Thanks,
Fabrice.